C++ Practice & Interview Zone
Multiple Choice Questions
- What is the correct syntax to declare a class in C++?
a) class myClass { };
b) Class myClass { }
c) class myClass;
d) struct myClass { };
Answer: a
- What is the default access specifier for members of a class in C++?
a) protected
b) private
c) public
d) static
Answer: b
- Which of the following is used to dynamically allocate memory in C++?
a) malloc
b) alloc
c) new
d) calloc
Answer: c
- Which operator is used to resolve scope in C++?
a) ::
b) .
c) ->
d) :
Answer: a
- What is the output of this code?
int x = 5;
cout << x++;
- a) 6
b) 5
c) Error
d) Undefined
Answer: b - Which keyword is used to inherit a class in C++?
a) inherit
b) extends
c) public
d) using
Answer: c
- What will this code output?
int x = 10;
int* p = &x;
cout << *p;
- a) address of x
b) garbage
c) 10
d) Error
Answer: c
- Which constructor is called when an object is created without arguments?
a) Copy constructor
b) Default constructor
c) Parameterized constructor
d) Virtual constructor
Answer: b
- Destructor name is same as constructor but prefixed with:
a) *
b) &
c) ~
d) #
Answer: c
- Which feature allows same function name with different arguments?
a) Operator Overloading
b) Function Overriding
c) Function Overloading
d) Inheritance
Answer: c
- What does STL stand for in C++?
a) Standard Testing Library
b) Standard Template Library
c) System Template Language
d) Source Template Library
Answer: b
- Which of the following is not a container in STL?
a) vector
b) list
c) map
d) math
Answer: d
- What is used to handle exceptions in C++?
a) if-else
b) try-catch
c) switch
d) error handler
Answer: b
- Which of these is a smart pointer in C++11?
a) auto_ptr
b) unique_ptr
c) dynamic_ptr
d) shared_obj
Answer: b
- What is the size of int on most 32-bit systems?
a) 2 bytes
b) 4 bytes
c) 8 bytes
d) 1 byte
Answer: b
- Function templates are used for:
a) memory management
b) exception handling
c) generic programming
d) runtime polymorphism
Answer: c
- Virtual functions are resolved at:
a) Compile time
b) Link time
c) Run time
d) Preprocessing
Answer: c
- Which container does not allow duplicate keys?
a) multimap
b) vector
c) set
d) multiset
Answer: c
- this pointer in C++ refers to:
a) base class
b) derived class
c) current object
d) none
Answer: c
- What is the output of this code?
int a = 4, b = 3;
cout << a % b;
- a) 0
b) 1
c) 2
d) Error
Answer: b
- Which of the following is a reference operator?
a) *
b) &
c) %
d) @
Answer: b
- Which keyword is used to define constant variables?
a) static
b) const
c) define
d) final
Answer: b
- What is the role of #include in C++?
a) Compilation
b) Linking
c) Header file inclusion
d) Debugging
Answer: c
- What will be the output?
cpp
CopyEdit
string s = “Hello”;
cout << s.length();
- a) 4
b) 5
c) 6
d) Error
Answer: b
- Which header is required for file handling?
a) <iostream>
b) <fstream>
c) <stdlib.h>
d) <stdio.h>
Answer: b
- Which of the following is used for multithreading?
a) <thread>
b) <pthread>
c) <mutex>
d) <threads>
Answer: a
- Which preprocessor directive is used to define macros?
a) #define
b) #macro
c) #include
d) #typedef
Answer: a
- Lambda expressions were introduced in which version of C++?
a) C++98
b) C++03
c) C++11
d) C++20
Answer: c
- Which is not a valid iterator type in STL?
a) forward_iterator
b) backward_iterator
c) random_access_iterator
d) bidirectional_iterator
Answer: b
- Which is used to allocate memory from heap in C++?
a) new
b) malloc
c) calloc
d) all of the above
Answer: d
- typeid is used for:
a) templates
b) type casting
c) type checking at runtime
d) exception handling
Answer: c
- What will the following code return?
cout << sizeof(char);
- a) 2
b) 4
c) 1
d) 8
Answer: c
- What is the correct syntax of namespace in C++?
a) namespace name { }
b) namespace : name { }
c) namespace => name { }
d) namespace(name) { }
Answer: a
- A class containing at least one pure virtual function is called:
a) base class
b) abstract class
c) virtual class
d) polymorphic class
Answer: b
- How many catch blocks can you have for a try block?
a) 1
b) 2
c) As many as needed
d) Only one
Answer: c
- What is std::pair used for?
a) Sorting
b) Memory
c) Storing two values
d) File I/O
Answer: c
- Which header file is used for vectors?
a) <vector>
b) <list>
c) <map>
d) <array>
Answer: a
- Which C++11 feature allows you to deduce variable type?
a) const
b) auto
c) static
d) volatile
Answer: b
- Which STL container follows FIFO?
a) stack
b) queue
c) vector
d) map
Answer: b
- unique_ptr belongs to which header?
a) <memory>
b) <utility>
c) <thread>
d) <pointer>
Answer: a