MCQS from C++ Programming Language set 25-50

  1. Which of the following best describes polymorphism in C++?
    a) Multiple functions with the same name but different parameters
    b) The ability of an object to take many forms
    c) Inheritance from multiple classes
    d) None of the above
    Answer: b) The ability of an object to take many forms

  1. Which keyword is used to prevent a function from being overridden in derived classes?
    a) private
    b) static
    c) final
    d) virtual
    Answer: c) final

  1. Which operator is used to allocate memory dynamically in C++?
    a) malloc
    b) allocate
    c) new
    d) mem_alloc
    Answer: c) new

  1. Which of the following correctly declares a pointer in C++?
    a) int ptr;
    b) int *ptr;
    c) pointer ptr;
    d) ptr int;
    Answer: b) int *ptr;

  1. What will happen if delete is used on a NULL pointer?
    a) Crash
    b) Compilation error
    c) No effect
    d) Undefined behavior
    Answer: c) No effect

  1. Which of the following is true about constructors in C++?
    a) They must have the same name as the class
    b) They can return values
    c) They are called explicitly
    d) None of the above
    Answer: a) They must have the same name as the class

  1. Which access specifier allows members to be inherited but not accessed outside the class hierarchy?
    a) public
    b) private
    c) protected
    d) final
    Answer: c) protected

  1. What is the default access specifier for members of a struct in C++?
    a) private
    b) public
    c) protected
    d) internal
    Answer: b) public

  1. Which of the following is NOT a valid C++ data type?
    a) bool
    b) float
    c) real
    d) double
    Answer: c) real

  1. What is the purpose of the friend keyword in C++?
    a) To allow one class to access private members of another class
    b) To create a pointer to an object
    c) To prevent inheritance
    d) To create a friend list
    Answer: a) To allow one class to access private members of another class

  1. Which keyword is used to define a class template in C++?
    a) class
    b) template
    c) typename
    d) both b and c
    Answer: d) both b and c

  1. Which of the following can be overloaded in C++?
    a) Operators
    b) Constructors
    c) Functions
    d) All of the above
    Answer: d) All of the above

  1. What is the output of 5 % 2 in C++?
    a) 0
    b) 1
    c) 2
    d) 5
    Answer: b) 1

  1. Which operator is used to access members of a structure using a pointer?
    a) . (dot operator)
    b) -> (arrow operator)
    c) :: (scope resolution)
    d) # (hash)
    Answer: b) -> (arrow operator)

  1. Which of the following is NOT a valid loop control statement in C++?
    a) break
    b) continue
    c) exit
    d) skip
    Answer: d) skip

  1. What will happen if a C++ program tries to divide by zero in integer division?
    a) Runtime error
    b) Compilation error
    c) Undefined behavior
    d) 0
    Answer: c) Undefined behavior

  1. Which header file is required to use the string class in C++?
    a) iostream
    b) string.h
    c) string
    d) stdio.h
    Answer: c) string

  1. Which of the following statements about destructors is true?
    a) They can be overloaded
    b) They are called when an object is destroyed
    c) They must return an int
    d) They take parameters
    Answer: b) They are called when an object is destroyed

  1. What does cin.ignore() do in C++?
    a) Ignores a specified number of characters from the input buffer
    b) Ignores the entire input
    c) Reads a line of input
    d) Clears the console
    Answer: a) Ignores a specified number of characters from the input buffer

  1. Which STL container stores elements in a key-value pair format?
    a) vector
    b) list
    c) map
    d) deque
    Answer: c) map

  1. Which function is used to find the length of a string in C++?
    a) strlen()
    b) length()
    c) size()
    d) Both b and c
    Answer: d) Both b and c

  1. What is the purpose of virtual functions in C++?
    a) To make a function accessible outside the class
    b) To allow function overriding in derived classes
    c) To optimize memory usage
    d) To prevent inheritance
    Answer: b) To allow function overriding in derived classes

  1. What is the default access level for members of a class in C++?
    a) private
    b) public
    c) protected
    d) None of the above
    Answer: a) private

  1. Which C++ feature allows multiple functions to have the same name but different parameters?
    a) Function overloading
    b) Function overriding
    c) Encapsulation
    d) Abstraction
    Answer: a) Function overloading

  1. Which of the following statements about static members in C++ is true?
    a) They are shared among all objects of a class
    b) They must be accessed using an object
    c) They are destroyed when an object goes out of scope
    d) None of the above
    Answer: a) They are shared among all objects of a class


Previous Post Next Post

نموذج الاتصال