C++ MCQ
C++ MCQ
a) Creating 3D models
b) Editing images
c) Designing websites
a) Playing audio
c) Creating animations
4. Which data type would you use to store a whole number in C++?
a) string
b) float
c) int
d) char
- Answer: c) int
a) +
b) /
c) -
d) *
- Answer: a) +
a) for loop
b) while loop
c) if statement
d) switch statement
12. How can you declare a function named "calculateSum" that takes two integer parameters and returns an integer in
C++?
a) Designing graphics
c) Creating 3D animations
d) Editing images
d) Declare a variable
a) Creating animations
c) Playing audio
d) Designing websites
a) char
b) int
c) float
d) string
- Answer: c) float
a) *
b) /
c) +
d) -
- Answer: a) *
20. How do you output text to the console in C++?
22. How do you repeatedly execute a block of code while a condition is true in C++?
a) for loop
b) while loop
c) if statement
d) switch statement
24. How do you declare a function named "calculateSum" that takes two integer parameters and returns an integer?
a) calculateSum(int a, int b): int { // code }
a) Designing graphics
c) Creating 3D animations
d) Editing images
d) Declare a variable
a) Creating animations
c) Playing audio
d) Designing websites
a) char
b) int
c) float
d) string
- Answer: c) float
a) *
b) /
c) +
d) -
- Answer: a) *
34. How do you repeatedly execute a block of code while a condition is true in C++?
a) for loop
b) while loop
c) if statement
d) switch statement
36. How do you declare a function named "calculateSum" that takes two integer parameters and returns an integer?
a) Designing graphics
c) Creating 3D animations
d) Editing images
d) Declare a variable
a) Creating animations
c) Playing audio
d) Designing websites
a) char
b) int
c) float
d) string
- Answer: c) float
a) *
b) /
c) +
d) -
- Answer: a) *
44. How do
46. How do you repeatedly execute a block of code while a condition is true in C++?
a) for loop
b) while loop
c) if statement
d) switch statement
48. How do you declare a function named "calculateSum" that takes two integer parameters and returns an integer?
a) Designing graphics
c) Creating 3D animations
d) Editing images
d) Declare a variable
Arrays:
a) A sequence of characters
b) A type of loop
d) A pointer to a function
- Explanation: An array is a data structure that holds a fixed number of elements of the same data type in a contiguous
memory location.
a) numbers[0]
b) numbers[1]
c) numbers.first()
d) first(numbers)
- Answer: a) numbers[0]
- Explanation: Array indices start from 0 in C++, so the first element is accessed using numbers[0].
a) 10
b) 9
c) 11
d) 0
- Answer: b) 9
- Explanation: Array indices range from 0 to size-1, so for an array of size 10, the maximum index is 9.
4. How do you initialize an array named "ages" with the values 18, 25, and 30?
Character Sequences:
a) char
b) string
c) int
d) float
- Answer: b) string
- Explanation: The string data type is used to represent character sequences in C++.
a) "hello" . "world"
b) "hello" + "world"
c) concat("hello", "world")
d) strcat("hello", "world")
a) 0
b) 1
c) Undefined
d) 10
- Answer: a) 0
a) programming[2]
b) programming[3]
c) programming.at(2)
d) getChar(programming, 3)
- Answer: a) programming[2]
- Explanation: String indices start from 0 in C++, so the third character is accessed using programming[2].
Pointers:
b) A type of loop
- Explanation: A pointer is a variable that holds the memory address of another variable.
d) numPtr = #
- Explanation: Pointers are declared by specifying the data type followed by an asterisk and the pointer variable name.
11. How do you access the value pointed to by a pointer named "ptr"?
a) ptr.value
b) *ptr
c) ptr->value
d) value(*ptr)
- Answer: b) *ptr
- Explanation: The `*` operator is used to dereference a pointer and access the value it points to.
- Explanation: The address-of operator '&' returns the memory address of a variable.
Pointers and Arrays:
- Explanation: An array name behaves like a pointer to the first element of the array.
14. How do you access the third element of an array using a pointer "arrPtr"?
a) arrPtr[2]
b) *arrPtr[2]
c) *(arrPtr + 2)
d) arrPtr + 2
- Answer: c) *(arrPtr + 2)
15. What is the correct way to initialize a pointer "ptr" to an array "numbers"?
a) ptr = &numbers;
b) ptr = numbers;
c) ptr = numbers[0];
d) ptr = *numbers;
- Explanation: To initialize a pointer to an array, use the address-of operator '&' with the array name.
Dynamic Memory:
- Explanation: Dynamic memory allocation allows you to allocate memory for variables at runtime using pointers.
17. How do you allocate memory for an integer using dynamic memory allocation?
a) new int;
d) int *
- Explanation: The `new` operator is used to dynamically allocate memory for a variable.
18. How do you deallocate memory allocated using the `new` operator?
a) free(pointer);
b) delete pointer;
c) clear(pointer);
d) deallocate(pointer);
- Explanation: The `delete` operator is used to deallocate memory allocated using the `new` operator.
- Explanation: A memory leak occurs when memory allocated using dynamic allocation is not deallocated properly.
Data Structures:
a) Array
b) Queue
c) Tree
d) Graph
- Answer: a) Array
- Explanation: An array organizes elements in a linear sequence, allowing easy access by index.
21. What is the main advantage of using linked lists over arrays?
- Explanation: Linked lists can grow or shrink dynamically, unlike arrays with fixed sizes.
- Explanation: The `push_back` function inserts an element at the end of a linked list.
d) A dynamic array
a) char
b) int
c) bool
d) string
- Answer: c) bool
c) bool = isReady(true);
d) isReady = true;
- Explanation: The correct syntax for declaring and initializing a bool variable is shown in option a.
Classes (I):
a) A loop construct
d) A type of array
- Explanation: A class is a blueprint for creating objects with specific properties and behaviors.
a) class Car;
b) class Car { };
b) Variables declared within a class but not associated with any object
- Answer: b) Variables declared within a class but not associated with any object
- Explanation: Member variables are data members declared within a class and are associated with objects of the
class.
Classes (II):
29. How do you define a constructor for a class named "Student" that takes two parameters, "name" and "age"?
a) Student(name, age) { }
b) Student::constructor(name, age) { }
30. How do you define a member function "displayInfo" for a class named "Person"?
a) displayInfo(Person) { }
b) Person::displayInfo() { }
c) displayInfo::Person() { }
d) void displayInfo() { }
- Answer: b) Person::displayInfo() { }
- Explanation: The correct syntax for defining a member function is shown in option b.
31. What is the access specifier for member functions that can be accessed from any class?
a) private
b) protected
c) public
d) global
- Answer: c) public
- Explanation: Member functions declared as public can be accessed from any class or object.
- Explanation: The "friend" keyword allows a function or another class to access private members of a class.
33. What is inheritance in C++?
- Explanation: Inheritance allows you to create new classes (derived classes) based on existing classes (base classes).
- Explanation: The base class is the class whose properties and behaviors are inherited by the derived class.
- Explanation: In C++, inheritance is represented using the ':' symbol followed by the access mode.
Polymorphism:
c) A technique that allows objects of different classes to be treated as objects of the same class
d) A type of loop
- Answer: c) A technique that allows objects of different classes to be treated as objects of the same class
- Explanation: Polymorphism allows objects of different classes to be used interchangeably through a common
interface.
37. What is function overloading in C++?
a) Defining the same function multiple times with different return types
b) Defining the same function multiple times with different parameter types or numbers
- Answer: b) Defining the same function multiple times with different parameter types or numbers
- Explanation: Function overloading allows you to define multiple functions with the same name but different
parameter lists.
a) Using templates
- Explanation: Runtime polymorphism is achieved using virtual functions and pointers to base class objects.
Absolutely, here are 50 multiple-choice questions about Preliminary Programming in C++, focusing on the topics you
mentioned, along with their correct answers and explanations:
Arrays:
a) int numbers[5];
b) array numbers[5];
c) numbers[5] = int[];
d) numbers = int[5];
- Explanation: The correct syntax for declaring an integer array with 5 elements is shown in option a.
a) scores[2];
b) scores[3];
c) scores(3);
d) scores.at(3);
- Answer: a) scores[2];
- Explanation: Arrays in C++ are zero-indexed, so the third element is accessed using scores[2].
a) 10
b) 9
c) 11
- Answer: a) 10
Character Sequences:
a) int
b) double
c) char
d) string
- Answer: c) char
- Explanation: The char data type is used to represent individual characters in C++.
5. How do you declare a C-style string named "name" with the value "John"?
d) name = "John";
- Explanation: C-style strings are declared using a character array with the null terminator at the end.
a) '\0'
b) '0'
c) NULL
d) '#'
- Answer: a) '\0'
- Explanation: The null terminator '\0' marks the end of a C-style string in C++.
Pointers:
a) A keyword
- Explanation: A pointer is a variable that holds the memory address of another variable.
a) int ptr;
b) int *ptr;
c) pointer ptr;
d) *ptr int;
9. How do you assign the address of an integer variable "num" to a pointer "ptr"?
a) ptr = #
b) ptr = num;
c) &ptr = num;
d) *ptr = num;
- Explanation: The address of "num" is assigned to the pointer "ptr" using the address-of operator &.
10. How do you access the value stored at the memory address pointed to by a pointer "ptr"?
a) ptr.value;
b) *ptr;
c) ptr;
d) value(*ptr);
- Answer: b) *ptr;
- Explanation: The value at the memory address pointed to by a pointer is accessed using the dereference operator *.
a) int *arrPtr[];
b) int arrPtr[];
c) int *arrPtr;
d) pointer arrPtr;
- Explanation: The correct syntax for declaring a pointer to an array of integers is shown in option c.
12. How do you access the second element of an array "data" using a pointer "ptr"?
a) ptr[1];
b) *ptr[2];
c) ptr[2];
d) &ptr[1];
- Answer: a) ptr[1];
- Explanation: Arrays and pointers are closely related, so you can use array subscript notation with pointers.
Dynamic Memory:
- Explanation: Dynamic memory allocation allows you to allocate memory at runtime using pointers.
14. How do you allocate memory for an integer using the "new" operator?
a) new int;
c) malloc(sizeof(int));
d) allocate int;
- Explanation: The "new" operator is used to dynamically allocate memory for a variable.
15. How do you deallocate memory allocated using the "new" operator?
a) free(pointer);
b) delete pointer;
c) clear(pointer);
d) deallocate(pointer);
- Explanation: The "delete" operator is used to deallocate memory allocated using the "new" operator.
Data Structures:
a) Array
b) Queue
c) Tree
d) Graph
- Answer: a) Array
- Explanation: An array organizes elements in a linear sequence, allowing easy access by index.
17. What is the main advantage of using linked lists over arrays?
- Explanation: Linked lists can grow or shrink dynamically, unlike arrays with fixed sizes.
- Explanation: To add an element at the end of a linked list, you need to update the last node's next pointer to point to
the new node.
19. Which data type is used to represent true or false values in C++?
a) int
b) double
c) char
d) bool
- Answer: d) bool
- Explanation: The bool data type is used to represent true or false values.
a) true
b) false
c) 1
d) 0
- Answer: b) false
- Explanation: The "&&" operator returns true only if both operands are true. In this case, one operand is true and the
other is false, so the result is false.
a) true
b) false
c) 1
d) 0
- Answer: a) true
- Explanation: The expression "5 > 3" is true because 5 is greater than 3.
Classes (I):
a) A type of function
- Explanation: A class is a blueprint for creating objects that contain both data and member functions.
23. How do you define a class named "Rectangle" with attributes "width" and "height"?
- Explanation: The correct syntax for defining a class with attributes is shown in option d.
25. How do you create an object of the class "Car" named "myCar"?
a) Car myCar();
b) Car myCar;
d) myCar = Car();
a) carObj.speed;
b) speed(carObj);
c) carObj.getSpeed();
d) getSpeed(carObj);
- Answer: a) carObj.speed;
Classes (II):
- Explanation: A constructor is a special member function that is automatically called when an object is created.
28. How do you define a constructor for the class "Student" with parameters "name" and "age"?
c) Student(name, age) { }
- Explanation: The correct syntax for defining a constructor with parameters is shown in option c.
- Explanation: A destructor is a member function with the same name as the class, preceded by a tilde (~), used for
cleanup tasks before an object is destroyed.
a) A type of loop
b) A type of attribute
d) A type of pointer
31. How do you declare a friend function named "printInfo" inside a class "Person"?
a) friend printInfo();
- Explanation: The correct syntax for declaring a friend function is shown in option b.
a) A type of variable
b) A relationship between classes where one class inherits properties and behaviors from another
c) A type of function
d) A type of array
- Answer: b) A relationship between classes where one class inherits properties and behaviors from another
- Explanation: Inheritance allows a class to inherit attributes and methods from another class.
d) The subclass
- Explanation: The base class is the class from which other classes inherit.
34. How do you indicate that a class "Child" is inheriting from a class "Parent"?
c) The subclass
- Explanation: The derived class is the class that inherits attributes and methods from another class.
Polymorphism:
b) A technique where different classes can be treated as instances of the same class through inheritance
d) A type of variable
- Answer: b) A technique where different classes can be treated as instances of the same class through inheritance
- Explanation: Polymorphism allows objects of different classes to be treated as if they belong to the same class.
c) A function declared with the "virtual" keyword that can be overridden in derived classes
- Answer: c) A function declared with the "virtual" keyword that can be overridden in derived classes
- Explanation: Virtual functions allow derived classes to provide their own implementations.
38. How do you declare a virtual function named "calculateArea" in a class "Shape"?
a) virtual calculateArea();
- Explanation: The correct syntax for declaring a virtual function is shown in option c.
Polymorphism (continued):
c) A technique for providing a new implementation for a virtual function in a derived class
- Answer: c) A technique for providing a new implementation for a virtual function in a derived class
- Explanation: Function overriding allows a derived class to provide its own implementation for a virtual function.
40. Which keyword is used to call the overridden function from the base class within a derived class?
a) super
b) base
c) this
- Answer: b) base
- Explanation: The "base" keyword is used to call the overridden function from the base class within a derived class.
41. Which data type is used to store decimal numbers with single precision?
a) int
b) double
c) float
d) decimal
- Answer: c) float
- Explanation: The float data type is used to store decimal numbers with single precision.
a) 2.5
b) 2
c) 2.0
d) 2.2
- Answer: a) 2.5
b) A technique for providing multiple implementations of a function with the same name in a class
- Answer: b) A technique for providing multiple implementations of a function with the same name in a class
- Explanation: Method overloading allows a class to have multiple functions with the same name but different
parameter lists.
44. How do you overload a function named "calculate" with two integer parameters in a class "Math"?
- Explanation: To overload a function, you provide multiple definitions with different parameter lists.
- Explanation: Multiple inheritance involves a class inheriting from two or more parent classes.
46. How do you resolve ambiguity in multiple inheritance if both parent classes have a function with the same name?
- Explanation: To resolve ambiguity, you can rename one of the functions in the derived class.
Polymorphism (continued):
b) A technique for providing multiple implementations of a function with the same name in a class
- Answer: b) A technique for providing multiple implementations of a function with the same name in a class
- Explanation: Function overloading allows a class to have multiple functions with the same name but different
parameter lists.
48. How do you overload the "+" operator for a class "Complex" to add two complex numbers?
- Explanation: To overload the "+" operator, you provide a function named "operator+" that takes two Complex objects
as parameters.
c) A situation where a derived class inherits from two classes that share a common base class
- Answer: c) A situation where a derived class inherits from two classes that share a common base class
- Explanation: The diamond problem occurs when a class inherits from two classes that have a common base class.
a) By using friendship
c) By creating an object
- Explanation: Inheritance allows you to create a derived class that "is-a" type of the base class.