Showing posts with label MCQ. Show all posts
Showing posts with label MCQ. Show all posts

Sunday, September 15, 2024

Computer Science MCQs with Answers

1. What is the value of the postfix expression 6 3 2 4 + – *?

a) 74
b) -18
c) 22
d) 40

Answer: b

Explanation: Postfix Expression is (6*(3-(2+4))) which results -18 as output.


2. Which of the following is not the application of stack?


a) Data Transfer between two asynchronous process
b) Compiler Syntax Analyzer
c) Tracking of local variables at run time
d) A parentheses balancing program

Answer: a

Explanation: Data transfer between the two asynchronous process uses the queue data structure for synchronisation. The rest are all stack applications.


3. Which data structure is used for implementing recursion?


a) Stack
b) Queue
c) List
d) Array

Answer: a

Explanation: Stacks are used for the implementation of Recursion.


4. Which header file is required in C++ to use OOP?


a) OOP can be used without using any header file
b) stdlib.h
c) iostream.h
d) stdio.h

Answer: a

Explanation: We need not include any specific header file to use OOP concept in C++, only specific functions used in code need their respective header files to be included or classes should be defined if needed.


5. Which of the following is not true about polymorphism?


a) Helps in redefining the same functionality
b) Increases overhead of function definition always
c) It is feature of OOP
d) Ease in readability of program

Answer: b

Explanation: It never increases function definition overhead, one way or another if you don’t use polymorphism, you will use the definition in some other way, so it actually helps to write efficient codes.


6. Which among the following can show polymorphism?


a) Overloading &&
b) Overloading <<
c) Overloading ||
d) Overloading +=

Answer: b

Explanation: Only insertion operator can be overloaded among all the given options. And the polymorphism can be illustrated here only if any of these is applicable of being overloaded. Overloading is type of polymorphism.


7. Which among the following is correct for the class defined below?


class student{

    int marks;

    public: student(){}

    student(int x){ 

         marks=x; 

    }

};

main(){

    student s1(100);

    student s2();

    student s3=100;

    return 0;

}


a) Program will give compile time error
b) Object s3, syntax error
c) Only object s1 and s2 will be created
d) Program runs and all objects are created


Answer: d

Explanation: It is a special case of constructor with only 1 argument. While calling a constructor with one argument, you are actually implicitly creating a conversion from the argument type to the type of class. Hence you can directly specify the value of that one argument with assignment operator.


8. What happens when an object is passed by reference?


a) Destructor is called at end of function
b) Destructor is called when called explicitly
c) Destructor is not called
d) Destructor is called when function is out of scope

Answer: c

Explanation: The destructor is never called in this situation. The concept is that when an object is passed by reference to the function, the constructor is not called, but only the main object will be used. Hence no destructor will be called at end of function.


9. How to access data members of a class?


a) Dot, arrow or direct call
b) Dot operator
c) Arrow operator
d) Dot or arrow as required

Answer: d

Explanation: The data members can never be called directly. Dot operator is used to access the members with help of object of class. Arrow is usually used if pointers are used.


10. Which feature of OOP reduces the use of nested classes?


a) Inheritance
b) Binding
c) Abstraction
d) Encapsulation

Answer: a

Explanation: Using inheritance we can have the security of the class being inherited. The subclass can access the members of parent class. And have more feature than a nested class being used.


11. Which operator can be used to free the memory allocated for an object in C++?


a) Unallocate
b) Free()
c) Collect
d) delete

Answer: d

Explanation: The delete operator in C++ can be used to free the memory and resources held by an object. The function can be called explicitly whenever required. In C++ memory management must be done by the programmer. There is no automatic memory management in C++.


12. Which of the following is not a property of an object?


a) Properties
b) Names
c) Identity
d) Attributes

Answer: b

Explanation: The names are not property of an object. The identity can be in any form like address or name of object but name can’t be termed as only identity of an object. The objects contain attributes that define what type of data an object can store.


13. How to access the private member function of a class?


a) Using class address
b) Using object of class
c) Using object pointer
d) Using address of member function

Answer: d

Explanation: Even the private member functions can be called outside the class. This is possible if address of the function is known. We can use the address to call the function outside the class.


14. Which among the following is not a necessary condition for constructors?


a) Its name must be same as that of class
b) It must not have any return type
c) It must contain a definition body
d) It can contains arguments

Answer: c

Explanation: Constructors are predefined implicitly, even if the programmer doesn’t define any of them. Even if the programmer declares a constructor, it’s not necessary that it must contain some definition.


15. If in multiple inheritance, class C inherits class B, and Class B inherits class A. In which sequence are their destructors called if an object of class C was declared?


a) ~A() then ~B() then ~C()
b) ~C() then ~A() then ~B()
c) ~C() then ~B() then ~A()
d) ~B() then ~C() then ~A()

Answer: c

Explanation: The destructors are always called in the reverse order of how the constructors were called. Here class A constructor would have been created first if Class C object is declared. Hence class A destructor is called at last.


16. Which feature in OOP is used to allocate additional functions to a predefined operator in any language?


a) Function Overloading
b) Function Overriding
c) Operator Overloading
d) Operator Overriding

Answer: c

Explanation: The feature is operator overloading. There is not a feature named operator overriding specifically. Function overloading and overriding doesn’t give addition function to any operator.


17. Which feature can be implemented using encapsulation?


a) Polymorphism
b) Overloading
c) Inheritance
d) Abstraction

Answer: d

Explanation: Data abstraction can be achieved by using encapsulation. We can hide the operation and structure of actual program from the user and can show only required information by the user.

Why does the Dijkstra algorithm fail to detect negative cycles?

Dijkstra’s algorithm fails to detect and correctly handle negative weight cycles due to the way it processes nodes and updates the shortest ...