Cpp Quiz
C++ Complete MCQ Quiz – 120 Questions
- One question will appear on the screen at a time.
- Use Next and Previous buttons to move between questions.
- Use the right-side question palette to jump to any question.
- Use Mark for Review for questions you want to revisit.
- The quiz auto-submits when time is over.
Question Palette
Yellow = Review
White = Not Answered
Answer key with explanations
Attempt the test and submit it first, then use this key to revise. Each answer has a short explanation so you understand why it is correct.
Show all 120 answers
- Which function is the starting point of every executable C++ program?
Answer: B. main() – main() is the entry point of a C++ program.
- Which header is commonly used for cout and cin?
Answer: B. <iostream> – iostream provides standard input and output streams.
- Which operator is used with cout?
Answer: B. << – cout uses insertion operator <<.
- Which operator is used with cin?
Answer: B. >> – cin uses extraction operator >>.
- What does return 0 generally indicate in main()?
Answer: B. Program completed successfully – return 0 means successful completion.
- Which extension is normally used for C++ source files?
Answer: A. .cpp – .cpp is the common extension for C++ source code.
- Which command can compile main.cpp using g++?
Answer: A. g++ main.cpp -o main – g++ main.cpp -o main compiles and names executable main.
- Which keyword creates an unchangeable variable value?
Answer: B. const – const makes a value read-only after initialization.
- Which data type stores true or false?
Answer: C. bool – bool stores true or false values.
- Which data type is best for decimal marks like 92.5?
Answer: B. double – double stores decimal values accurately enough for common calculations.
- Which is a valid C++ variable name?
Answer: C. total_marks – total_marks is valid; spaces and starting with digit are invalid, class is keyword.
- Which symbol ends most C++ statements?
Answer: B. ; – Semicolon ends most statements.
- Which comment is single-line in C++?
Answer: A. // comment – // creates a single-line comment.
- What is namespace std used for?
Answer: A. To access standard library names – std contains cout, cin, string, vector and many standard names.
- Which is the correct way to print Hello?
Answer: C. cout << "Hello"; – cout << value prints output.
- What is integer division result of 5 / 2 in C++ when both operands are int?
Answer: B. 2 – Integer division removes decimal part.
- Which operator gives remainder?
Answer: B. % – % gives remainder of integer division.
- Which operator checks equality?
Answer: B. == – == compares two values for equality.
- Which logical operator means AND?
Answer: A. && – && is logical AND.
- Which logical operator means OR?
Answer: B. || – || is logical OR.
- Which statement is best for range checking marks?
Answer: A. if-else – if-else handles ranges like marks >= 90.
- Which keyword handles unmatched switch cases?
Answer: B. default – default handles unmatched switch values.
- Why is break used in switch?
Answer: B. To prevent fall-through – break stops execution from continuing into next case.
- How many times does for(int i=1;i<=3;i++) run?
Answer: C. 3 – i takes values 1,2,3.
- Which loop executes at least once?
Answer: C. do-while – do-while checks condition after body.
- What does continue do?
Answer: B. Skips current iteration – continue skips remaining body for current iteration.
- What does break do inside a loop?
Answer: B. Terminates loop – break exits the nearest loop.
- Array indexing in C++ starts from
Answer: A. 0 – C++ arrays use zero-based indexing.
- If int a[5]; valid last index is
Answer: B. 4 – For size 5, indexes are 0 to 4.
- Which header is used for C++ string class?
Answer: A. <string> – <string> provides std::string.
- Which function reads a full line into a string?
Answer: B. getline – getline(cin, str) reads spaces too.
- A function that returns no value uses return type
Answer: B. void – void means no return value.
- Which is true about recursion?
Answer: A. Function calls itself – Recursive function calls itself and needs base case.
- Base case in recursion is used to
Answer: B. Stop recursion – Base case stops recursive calls.
- A reference is best described as
Answer: A. Alias for another variable – Reference is another name for an existing variable.
- Which symbol is used to get address of a variable?
Answer: B. & – & gives address of a variable.
- Which symbol dereferences a pointer?
Answer: B. * – *p gives value at address stored in p.
- Which value should be used for a pointer that points nowhere?
Answer: B. nullptr – nullptr is modern C++ null pointer.
- Which keyword allocates dynamic memory in C++?
Answer: B. new – new allocates memory in C++.
- Which keyword releases memory allocated by new?
Answer: B. delete – delete releases memory allocated by new.
- A memory leak happens when
Answer: A. Memory is allocated and not released – Not releasing dynamic memory causes a memory leak.
- Which keyword defines a structure?
Answer: A. struct – struct defines a structure.
- What must come after a class or struct definition?
Answer: B. Semicolon – Class/struct definitions end with semicolon.
- Function overloading requires functions to have
Answer: A. Same name, different parameter list – Overloaded functions share name but differ in parameter list.
- Can function overloading be done by return type alone?
Answer: B. No – Return type alone is not enough for overloading.
- Default arguments are used when
Answer: A. Caller omits some arguments – Default parameter values are used if caller does not pass value.
- A static local variable
Answer: B. Retains value between calls – static local variable keeps its value between calls.
- Which header is used for file streams?
Answer: A. <fstream> – fstream provides ifstream, ofstream and fstream.
- Which stream writes to a file?
Answer: B. ofstream – ofstream is output file stream.
- Which stream reads from a file?
Answer: A. ifstream – ifstream is input file stream.
- A class is
Answer: A. Blueprint for objects – Class defines data and functions for objects.
- An object is
Answer: A. Instance of a class – Object is created from a class.
- Which access specifier hides members from outside class?
Answer: B. private – private members cannot be accessed directly outside class.
- Default access in a C++ class is
Answer: B. private – Class members are private by default.
- Default access in a C++ struct is
Answer: C. public – Struct members are public by default.
- Constructor name is same as
Answer: B. Class name – Constructor has same name as class.
- Constructor return type is
Answer: C. No return type – Constructor has no return type.
- Destructor name begins with
Answer: B. ~ – Destructor name is ~ClassName.
- Which pointer refers to current object?
Answer: B. this – this points to current object.
- Static data member belongs to
Answer: A. Class – Static member is shared at class level.
- Friend function can access
Answer: B. Private members if declared friend – friend function can access private/protected members.
- Inheritance supports
Answer: A. Code reuse – Inheritance reuses and extends base class features.
- The class inherited from is called
Answer: B. Base class – Base class is parent class.
- The inheriting class is called
Answer: A. Derived class – Derived class inherits from base class.
- Which inheritance mode keeps public members public?
Answer: C. public – Public inheritance keeps public members public.
- Polymorphism means
Answer: A. Many forms – Polymorphism means one interface with many forms.
- Run-time polymorphism is achieved using
Answer: A. Virtual functions – virtual functions support dynamic binding.
- Pure virtual function syntax includes
Answer: A. = 0 – Pure virtual function ends with = 0.
- A class with pure virtual function is
Answer: A. Abstract class – It becomes an abstract class.
- Operator overloading is used for
Answer: A. User-defined meaning of operators – Operator overloading defines operator behavior for classes.
- Which operator cannot be overloaded?
Answer: C. :: – Scope resolution operator :: cannot be overloaded.
- Templates enable
Answer: A. Generic programming – Templates work with different data types generically.
- Which keyword starts a function template?
Answer: A. template – template starts template declaration.
- Exception handling uses
Answer: A. try, throw, catch – try-throw-catch handles exceptions.
- Which keyword throws an exception?
Answer: B. throw – throw raises an exception.
- Which keyword handles an exception?
Answer: A. catch – catch handles thrown exception.
- STL stands for
Answer: A. Standard Template Library – STL means Standard Template Library.
- Which STL container is dynamic array?
Answer: A. vector – vector is dynamic array.
- Which vector function adds element at end?
Answer: A. push_back – push_back appends element.
- Which STL container stores unique sorted values?
Answer: A. set – set stores unique sorted keys.
- Which STL container stores key-value pairs?
Answer: A. map – map stores key-value pairs.
- Which data structure follows LIFO?
Answer: A. Stack – Stack follows Last In First Out.
- Which data structure follows FIFO?
Answer: B. Queue – Queue follows First In First Out.
- Which function gets top element of stack?
Answer: C. top() – stack uses top().
- Which algorithm sorts a vector?
Answer: A. sort() – sort(begin,end) sorts a range.
- Which header contains sort()?
Answer: A. <algorithm> – sort is declared in <algorithm>.
- Which iterator points to first element?
Answer: A. begin() – begin() returns iterator to first element.
- Which iterator points after the last element?
Answer: B. end() – end() points one past last element.
- Range-based for loop syntax uses
Answer: A. for(auto x : container) – C++ range-for uses colon.
- auto keyword is used to
Answer: A. Let compiler infer type – auto deduces type from initializer.
- lambda expression is
Answer: A. Unnamed function – Lambda is an anonymous function object.
- Smart pointers are available in
Answer: A. <memory> – <memory> provides smart pointers.
- unique_ptr means
Answer: A. Single ownership – unique_ptr has exclusive ownership.
- shared_ptr means
Answer: A. Shared ownership – shared_ptr shares ownership through reference counting.
- RAII is mainly about
Answer: A. Resource management through object lifetime – RAII ties resource release to object destruction.
- binary_search requires range to be
Answer: A. Sorted – binary_search expects sorted range.
- Which function reverses a range?
Answer: A. reverse() – reverse(begin,end) reverses order.
- Which program design is best for a student management project?
Answer: A. Use classes and file handling – Classes and files make project organized.
- Encapsulation means
Answer: A. Wrapping data and methods together with controlled access – Encapsulation protects data inside class.
- Abstraction means
Answer: A. Showing essential details and hiding internal complexity – Abstraction hides implementation details.
- Which concept represents is-a relationship?
Answer: A. Inheritance – Inheritance models is-a relationship.
- Which concept represents has-a relationship?
Answer: A. Composition – Composition models has-a relationship.
- What is dangling pointer?
Answer: A. Pointer pointing to invalid/freed memory – Dangling pointer points to memory that is no longer valid.
- Which is safer in modern C++?
Answer: A. Smart pointer – Smart pointers reduce memory management errors.
- Which cast is preferred for normal explicit conversion?
Answer: A. static_cast – static_cast is preferred for well-defined conversions.
- Which value prints for bool true by default using cout?
Answer: B. 1 – cout prints true as 1 unless boolalpha is used.
- Which manipulator prints bool as true/false text?
Answer: A. boolalpha – boolalpha changes bool output formatting.
- Which manipulator sets decimal precision?
Answer: A. setprecision – setprecision is from <iomanip>.
- Which header is needed for setprecision?
Answer: A. <iomanip> – iomanip contains formatting manipulators.
- Which operator accesses class member through object?
Answer: A. . – Dot operator accesses member using object.
- Which operator accesses member through pointer?
Answer: B. -> – Arrow operator accesses member through pointer.
- Which operator is scope resolution?
Answer: A. :: – :: is scope resolution operator.
- Which keyword prevents overriding when used on virtual function in derived class?
Answer: A. final – final prevents further overriding.
- Which keyword clearly marks overriding function?
Answer: A. override – override tells compiler function should override a base virtual function.
- Which container adaptor gives highest priority element first by default?
Answer: A. priority_queue – priority_queue returns highest priority element first by default.
- Which pair member stores first value?
Answer: A. first – pair uses first and second.
- Which pair member stores second value?
Answer: B. second – pair second stores second value.
- Which STL container is best for fast key lookup with sorted keys?
Answer: A. map – map keeps keys sorted and supports lookup.
- Which container can store duplicates in sorted order?
Answer: A. multiset – multiset allows duplicate sorted values.
- Which keyword defines a type alias in modern C++?
Answer: A. using – using can create type aliases.
