[go: up one dir, main page]

0% found this document useful (0 votes)
35 views12 pages

MCQ of CPP

Uploaded by

Diptimayee Rana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views12 pages

MCQ of CPP

Uploaded by

Diptimayee Rana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

1. Which of the following is not a fundamental data type in C++?

A) int
B) float
C) string
D) char
Answer: C) string

2. What is the output of the following code snippet?


#include <iostream>
using namespace std;
int main( )
{
int x = 10;
int &y = x;
y = 20;
cout << x << endl;
return 0;
}
A) 10
B) 20
C) Compilation Error
D) Undefined Behavior
Answer: B) 20

3. What is the result of the following code snippet?


#include <iostream>
using namespace std;
int main( )
{
int arr[5] = {1, 2, 3, 4, 5};
int *ptr = arr;
cout << *(ptr + 2) << endl;
return 0;
}
A) 1
B) 2
C) 3
D) 4
Answer: C) 3

4. Which operator is used for dynamic memory allocation in C++?


A) new
B) malloc
C) alloc
D) alloc_mem
Answer: A) new
5. Which statement is true about C++ references?

A) References cannot be null.


B) References can be re-assigned to refer to different variables after initialization.
C) References occupy additional memory space compared to pointers.
D) References are used for dynamic memory allocation.
Answer: A) References cannot be null.

6. Which among the following statements is correct regarding ‘iostream’ and ‘cstdio’ in C++?

A) ‘iostream’ and ‘cstdio’ both provide functions for console input and output.

B) ‘iostream’ and ‘cstdio’ are interchangeable and can be used interchangeably in any C++ program.

C) ‘iostream’ is used for console input and output, while ‘cstdio’ is used for file input and output.

D) ‘cstdio’ is used for console input and output, while ‘iostream’ is used for file input and output.

Answer: C) ‘iostream’ is used for console input and output, while ‘cstdio’ is used for file input
and output.

7. What does the following code snippet output?


#include <iostream>
using namespace std;
int main()
{
int x = 5;
int *ptr = &x;
cout << *ptr << endl;
*ptr = 10;
cout << x << endl;
return 0;
}
A) 5, 10
B) 10, 10
C) 5, 5
D) 10, 5
Answer: A) 5, 10

8. What will be the output of the following code snippet?


#include <iostream>
using namespace std;
int main() {
int arr[] = {1, 2, 3, 4, 5};
int *ptr = arr;
cout << ptr[3] << endl;
return 0;
}
A) 1
B) 2
C) 3
D) 4
Answer: D) 4

9. What is the output of the following code snippet?


#include <iostream>
using namespace std;
void modifyArray(int arr[ ])
{
arr[0] = 10;
}
int main( )
{
int arr[ ] = {1, 2, 3, 4, 5};
modifyArray(arr);
cout << arr[0] << endl;
return 0;
}
A) 1
B) 2
C) 3
D) 10
Answer: D) 10

10. Which operator is used to access the memory address of a variable in C++?
A) & (address-of operator)
B) * (dereference operator)
C) -> (arrow operator)
D) . (dot operator)
Answer: A) & (address-of operator)

11. Wrapping data and its related functionality into a single entity is known as :-

a) Abstraction
b) Encapsulation
c) Polymorphism
d) Modularity
Answer: b) Encapsulation
12. Which of the following explains Polymorphism?

a) int func(int, int); float func1(float, float);


b) int func(int); int func(int);
c) int func(float); float func(int, int, char);
d) int func(); int new_func();

Answer: c) int func(float); float func(int, int, char);

13.Which of the following is true about the ‘virtual’ keyword in C++?


A) It specifies that a function cannot be overridden in derived classes.
B) It is used to declare a function in a base class.
C) It indicates that a function can be overridden in derived classes.
D) It is used to declare a function as private.
Answer: C) It indicates that a function can be overridden in derived classes.

14. Which type of inheritance in C++ allows a class to inherit from multiple base classes?
A) Single Inheritance
B) Multiple Inheritance
C) Hierarchical Inheritance
D) Multilevel Inheritance
Answer: B) Multiple Inheritance
15. What will be the output of the following code snippet?
#include <iostream>
using namespace std;
class Base
{
public:
virtual void display( )
{
cout << “Base Display” << endl;
}
};
class Derived : public Base
{
public:
void display( ) override
{
cout << “Derived Display” << endl;
}
};
int main( )
{
Base obj;
Derived* ptr = dynamic_cast<Derived*>(&obj);
if (ptr)
{
ptr->display( );
} else
{
cout << “Null Pointer” << endl;
}
return 0;
}
A) Base Display
B) Derived Display
C) Null Pointer
D) Compilation Error

Answer: C) Null Pointer

16. Which of the following is not a type of Constructor?

a) Friend constructor
b) Copy constructor
c) Default constructor
d) Parameterized constructor

Answer: a) Friend constructor

17. Which of the following cannot be used with the virtual keyword?

a) Class
b) Member functions
c) Constructors
d) Destructors

Answer: c) Constructors
18. What is the role of ‘try’, ‘catch’, and ‘throw’ in exception handling in C++?

A) ‘try’ is used to handle exceptions, ‘catch’ is used to throw exceptions, and ‘throw’ is used to begin
exception handling.

B) ‘try’ is used to begin exception handling, ‘catch’ is used to handle exceptions, and ‘throw’ is used
to throw exceptions.

C) ‘try’ is used to throw exceptions, ‘catch’ is used to begin exception handling, and ‘throw’ is used
to handle exceptions.

D) ‘try’, ‘catch’, and ‘throw’ are unrelated to exception handling in C++.

Answer: B) ‘try’ is used to begin exception handling, ‘catch’ is used to handle exceptions, and
‘throw’ is used to throw exceptions.

19. What is the advantage of exception handling ?


a) Remove error-handling code from the software's main line of code.
b) A method writer can choose to handle certain exceptions and delegate others to the caller.
c) An exception that occurs in a function can be handled anywhere in the function call stack.

i ) Only a
ii ) a, b and c
iii ) a and c
iv ) a and b
Answer: ii ) a, b and c

20. Uncaught exception leads to ______________.


a) termination of program
b) successful execution of programs
c) no effect on the program
d) execution of other functions of the program starts

Answer: a) termination of program

21. What is time complexity of fun( )?

int fun(int n)
{
int count = 0;
for (int i = n; i > 0; i /= 2)
for (int j = 0; j < i; j++)
count += 1;
return count;
}
a) O(n2 )
b) O(n*log(n))
c) O(n )
d) O(n*log(n*log(n)))
Answer: b) O(n*log(n))
22. O( n2 ) is the worst case time complexity, so among the given options it can represent :-
a) O( n )
b) O( 1 )
c) O ( nlogn )
d) All of the above
e) Answer: d) All of the above
23. What is the time complexity of the below function?
void fun(int n, int arr[])
{
int i = 0, j = 0;
for (; i < n; ++i)
while (j < n && arr[i] < arr[j])
j++;
}
a) O(n)
b) O(n2)
c) O(n*log(n))
d) O(n*log(n)2)
Answer: (a) O(n)

24. The time complexity of enqueue operation in Queue is __

a. O(1)
b. O(n)
c. O(logn)
d. O(nlogn)

Answer: a ) O(1)

25. What is the time complexity of the binary search algorithm?


a) O(n)
b) O(1)
c) O(log 2 n)
d) O(n2)
Answer: c ) O(log 2 n)

26. Which of the following is incorrect? Algorithms can be represented:


a) As programs

b) As Flow charts

c) As Syntax

d) As Pseudo Codes

Answer: c ) As Syntax

27. In algorithm analysis, what does "Big O" notation represent?

a) Best-case complexity

b) Average-case complexity

c) Worst-case complexity

d) All of the above

Answer: c) Worst-case complexity

28. In algorithm analysis, what does "Amortized Analysis" refer to?

a) Analyzing the average-case time complexity of an algorithm

b) Analyzing the worst-case time complexity of an algorithm

c) Analyzing the average-case space complexity of an algorithm

d) Analyzing the average-case time complexity over a sequence of operations

Answer: d) Analyzing the average-case time complexity over a sequence of operations

29. Which notation is commonly used to express the worst-case time complexity of an

algorithm? a) O(n)

b) Ω(n)

c) Θ(n)

d) O(log n)

Answer: a) O(n)

30. What are the characteristics of a good algorithm?


a) Correctness, efficiency, clarity
b) Speed, complexity, brevity
c) Flexibility, innovation, simplicity
d) Precision, optimization, adaptability
Answer: a) Correctness, efficiency, clarity
31. Which STL container is typically used when fast insertion and deletion at the end of the
container are required?
a) Vector
b) List
c) Set
d) Map
Answer: a ) Vector

32. Which container class in the STL allows constant time insertion and deletion operations at
both the beginning and end of the container?
a) Stack
b) Queue
c) Deque
d) Priority queue
Answer: c ) Deque

33. Which STL container class is implemented as a dynamic array and provides random access
iterators?
a) Vector
b) List
c) Set
d) Map
Answer: a ) Vector

34. What type of iterator does a list in the STL typically use?
a) Random access iterator
b) Bidirectional iterator
c) Forward iterator
d) Input iterator
Answer: b) Bidirectional iterator

35. Which operation is commonly associated with a priority queue in the STL?
a) FIFO (First-In-First-Out)
b) LIFO (Last-In-First-Out)
c) Sorting elements based on a comparison function
d) Retrieving the element with the highest priority
Answer: d) Retrieving the element with the highest priority
36. Which STL container is typically implemented as a FIFO (First-In-First-Out) data
structure?
a) Stack
b) Queue
c) Deque
d) Priority queue
Answer: b ) Queue

37. What is the purpose of a bit set container in the STL?


a) Storing a sequence of bits efficiently
b) Storing elements in sorted order
c) Implementing a LIFO data structure
Associating keys with values
Answer: a ) Storing a sequence of bits efficiently

38. What does an iterator in the STL represent?


a) An index pointing to an element in a container
b) A pointer to a container
c) A reference to a container element
d) An integer representing the size of the container
Answer: a) An index pointing to an element in a container

39. Which STL container class automatically sorts its elements based on a specified comparison
function?
a) Vector
b) List
c) Set
d) Map
Answer: c) Set
40. Which method is used to retrieve the number of elements in a std::map?
a) count( )
b) size( )
c) length( )
d) elements( )
Answer: b ) size ( )

41. Recursion is a method in which the solution of a problem depends on ____________ .


a) Larger instances of different problems
b) Larger instances of the same problem
c) Smaller instances of the same problem
d) Smaller instances of different problem

Answer: c ) Smaller instances of the same problem

42. Which of the following problems can’t be solved using recursion?


a) Factorial of a number
b) Nth fibonacci number
c) Length of a string
d) Problems without base case
Answer: d ) Problems without base case

43. Predict output of following program.


#include <iostream>
Using namespace std;
int fun(int n)
{
if (n == 4)
return n;
else
return 2*fun(n+1);
}
int main( )
{
cout<< fun(2);
return 0;
}
a) 4
b) 8
c)16
d )Runtime Error

Answer: c ) 16

44. What happens when the backtracking algorithm reaches a complete solution?
a) It backtracks to the root
b) It continues searching for other possible solutions
c) It traverses from a different route
d) Recursively traverses through the same route
Answer: b) It continues searching for other possible solutions

45. Which one of the following is an application of the backtracking algorithm?


a) Finding the shortest path
b) Finding the efficient quantity to shop
c) Ludo
d) Crossword
Answer: d) Crossword
46. Which of the following lines should be inserted to complete the above code?
a) fibo(n – 1)
b) fibo(n – 1) + fibo(n – 2)
c) fibo(n) + fibo(n – 1)
d) fibo(n – 2) + fibo(n – 1)
Answer: b ) fibo(n – 1) + fibo(n – 2)
47. If GCD of two numbers is 1, then the two numbers are said to be ________.
a) Co-prime numbers
b) Prime numbers
c) Composite numbers
d) Rational numbers
Answer: a) Co-prime number
48. Which algorithm is commonly used to find the greatest common divisor (GCD) of two
numbers?
a) Sieve of Eratosthenes
b) Euclidean algorithm
c) Fermat's factorization method
d) Pollard's rho algorithm
Answer: b) Euclidean algorithm
49. Which data structure is commonly used to implement the Sieve of Eratosthenes algorithm?
a) Array
b) Linked list
c) Binary search tree
d) Hash table
Answer: a ) Array
50. Which of the following statements is true about the Sieve of Eratosthenes algorithm?
a) It generates all prime numbers up to a given limit
b) It generates all prime factors of a given number
c) It generates all composite numbers up to a given limit
d) It generates all divisors of a given number
Answer: a ) It generates all prime numbers up to a given limit

You might also like