0% found this document useful (0 votes)
253 views34 pages

C++ MCQ

The document contains 50 multiple-choice questions about preliminary programming in C++ for the first semester. The questions cover topics such as the purpose of C++, program structure, variables, data types, functions, conditionals, and loops. The main purpose of C++ programming addressed is writing and executing computer programs.

Uploaded by

Arian SS
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
253 views34 pages

C++ MCQ

The document contains 50 multiple-choice questions about preliminary programming in C++ for the first semester. The questions cover topics such as the purpose of C++, program structure, variables, data types, functions, conditionals, and loops. The main purpose of C++ programming addressed is writing and executing computer programs.

Uploaded by

Arian SS
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 34

50 multiple-choice questions about Preliminary Programming in C++, 1st Semester

1. What is the purpose of the C++ programming language?

a) Creating 3D models

b) Editing images

c) Designing websites

d) Writing and executing computer programs

- Answer: d) Writing and executing computer programs

2. Which of the following represents the correct structure of a C++ program?

- Answer: b) main() { cout << "Hello, World!"; }

3. What are variables used for in programming?

a) Playing audio

b) Storing data and values

c) Creating animations

d) Designing user interfaces

- Answer: b) Storing data and values

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

5. What is the scope of a variable in C++?

a) The range of values it can hold

b) The location in memory where it's stored


c) The part of the program where it's accessible

d) The data type it belongs to

- Answer: c) The part of the program where it's accessible

6. What is the purpose of constants in C++?

a) Storing user input

b) Storing changing values

c) Storing unchangeable values

d) Storing text data

- Answer: c) Storing unchangeable values

7. Which operator is used to perform addition in C++?

a) +

b) /

c) -

d) *

- Answer: a) +

8. How would you output "Hello, World!" to the console in C++?

- Answer: b) cout << "Hello, World!";

9. What is the correct syntax for a simple if statement in C++?

- Answer: c) if (condition) { /* code */ }


10. How would you execute a block of code repeatedly as long as a condition is true in C++?

a) for loop

b) while loop

c) if statement

d) switch statement

- Answer: b) while loop

11. What does the return type of a function indicate?

a) The variable type to use

b) The number of arguments

c) The type of the first parameter

d) The data type of the value the function returns

- Answer: d) The data type of the value the function returns

12. How can you declare a function named "calculateSum" that takes two integer parameters and returns an integer in
C++?

a) calculateSum(int a, int b): int { // code }

b) int calculateSum(a, b) { // code }

c) int calculateSum(int a, int b) { // code }

d) calculateSum(a: int, b: int) -> int { // code }

- Answer: c) int calculateSum(int a, int b) { // code }

13. What is the primary purpose of C++ programming?

a) Designing graphics

b) Writing and executing computer programs

c) Creating 3D animations

d) Editing images

- Answer: b) Writing and executing computer programs

14. How do you start writing a C++ program?

a) Open a text editor

b) Create a new folder

c) Design a user interface

d) Declare a variable

- Answer: a) Open a text editor


15. What is the role of variables in programming?

a) Creating animations

b) Storing data and values

c) Playing audio

d) Designing websites

- Answer: b) Storing data and values

16. Which data type is used to store decimal numbers in C++?

a) char

b) int

c) float

d) string

- Answer: c) float

17. What is the scope of a variable in programming?

a) The range of values it can hold

b) The location in memory where it's stored

c) The part of the program where it's accessible

d) The data type it belongs to

- Answer: c) The part of the program where it's accessible

18. What is the purpose of constants in C++?

a) Storing user input

b) Storing changing values

c) Storing unchangeable values

d) Storing text data

- Answer: c) Storing unchangeable values

19. Which operator is used for multiplication in C++?

a) *

b) /

c) +

d) -

- Answer: a) *
20. How do you output text to the console in C++?

- Answer: c) cout << "Hello, World!";

21. What is the correct syntax for a simple if statement in C++?

- Answer: c) if (condition) { /* code */ }

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

- Answer: b) while loop

23. What does the return type of a function specify?

a) The variable type to use

b) The number of arguments

c) The type of the first parameter

d) The type of the value the function returns

- Answer: d) The type of the value the function returns

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 }

b) int calculateSum(a, b) { // code }

c) int calculateSum(int a, int b) { // code }

d) calculateSum(a: int, b: int) -> int { // code }

- Answer: c) int calculateSum(int a, int b) { // code }

25. What is the main purpose of C++ programming?

a) Designing graphics

b) Writing and executing computer programs

c) Creating 3D animations

d) Editing images

- Answer: b) Writing and executing computer programs

26. How do you start writing a C++ program?

a) Open a text editor

b) Create a new folder

c) Design a user interface

d) Declare a variable

- Answer: a) Open a text editor

27. What is the role of variables in programming?

a) Creating animations

b) Storing data and values

c) Playing audio

d) Designing websites

- Answer: b) Storing data and values

28. Which data type is used to store decimal numbers in C++?

a) char

b) int

c) float

d) string

- Answer: c) float

29. What is the scope of a variable in programming?


a) The range of values it can hold

b) The location in memory where it's stored

c) The part of the program where it's accessible

d) The data type it belongs to

- Answer: c) The part of the program where it's accessible

30. What is the purpose of constants in C++?

a) Storing user input

b) Storing changing values

c) Storing unchangeable values

d) Storing text data

- Answer: c) Storing unchangeable values

31. Which operator is used for multiplication in C++?

a) *

b) /

c) +

d) -

- Answer: a) *

32. How do you output text to the console in C++?

- Answer: c) cout << "Hello, World!";

33. What is the correct syntax for a simple if statement in C++?


- Answer: c) if (condition) { /* code */ }

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

- Answer: b) while loop

35. What does the return type of a function specify?

a) The variable type to use

b) The number of arguments

c) The type of the first parameter

d) The type of the value the function returns

- Answer: d) The type of the value the function returns

36. 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 }

b) int calculateSum(a, b) { // code }

c) int calculateSum(int a, int b) { // code }

d) calculateSum(a: int, b: int) -> int { // code }

- Answer: c) int calculateSum(int a, int b) { // code }

37. What is the main purpose of C++ programming?

a) Designing graphics

b) Writing and executing computer programs

c) Creating 3D animations

d) Editing images

- Answer: b) Writing and executing computer programs


38. How do you start writing a C++ program?

a) Open a text editor

b) Create a new folder

c) Design a user interface

d) Declare a variable

- Answer: a) Open a text editor

39. What is the role of variables in programming?

a) Creating animations

b) Storing data and values

c) Playing audio

d) Designing websites

- Answer: b) Storing data and values

40. Which data type is used to store decimal numbers in C++?

a) char

b) int

c) float

d) string

- Answer: c) float

41. What is the scope of a variable in programming?

a) The range of values it can hold

b) The location in memory where it's stored

c) The part of the program where it's accessible

d) The data type it belongs to

- Answer: c) The part of the program where it's accessible

42. What is the purpose of constants in C++?

a) Storing user input

b) Storing changing values

c) Storing unchangeable values

d) Storing text data

- Answer: c) Storing unchangeable values


43. Which operator is used for multiplication in C++?

a) *

b) /

c) +

d) -

- Answer: a) *

44. How do

you output text to the console in C++?

- Answer: c) cout << "Hello, World!";

45. What is the correct syntax for a simple if statement in C++?

- Answer: c) if (condition) { /* code */ }

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

- Answer: b) while loop


47. What does the return type of a function specify?

a) The variable type to use

b) The number of arguments

c) The type of the first parameter

d) The type of the value the function returns

- Answer: d) The type of the value the function returns

48. 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 }

b) int calculateSum(a, b) { // code }

c) int calculateSum(int a, int b) { // code }

d) calculateSum(a: int, b: int) -> int { // code }

- Answer: c) int calculateSum(int a, int b) { // code }

49. What is the main purpose of C++ programming?

a) Designing graphics

b) Writing and executing computer programs

c) Creating 3D animations

d) Editing images

- Answer: b) Writing and executing computer programs

50. How do you start writing a C++ program?

a) Open a text editor

b) Create a new folder

c) Design a user interface

d) Declare a variable

- Answer: a) Open a text editor


50 multiple-choice questions about Preliminary Programming in C++, 2nd Semester

Arrays:

1. What is an array in C++?

a) A sequence of characters

b) A type of loop

c) A collection of variables of the same type

d) A pointer to a function

- Answer: c) A collection of variables of the same type

- Explanation: An array is a data structure that holds a fixed number of elements of the same data type in a contiguous
memory location.

2. How do you access the first element of an array named "numbers"?

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].

3. What is the maximum index for an array of size 10?

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?

a) ages = {18, 25, 30};

b) ages[] = {18, 25, 30};

c) int ages[] = {18, 25, 30};

d) ages[18, 25, 30];

- Answer: c) int ages[] = {18, 25, 30};


- Explanation: The correct syntax for initializing an array is to specify its data type and then provide the values in curly
braces.

Character Sequences:

5. Which data type is used to represent character sequences in C++?

a) char

b) string

c) int

d) float

- Answer: b) string

- Explanation: The string data type is used to represent character sequences in C++.

6. How do you concatenate two strings "hello" and "world"?

a) "hello" . "world"

b) "hello" + "world"

c) concat("hello", "world")

d) strcat("hello", "world")

- Answer: a) "hello" . "world"

- Explanation: The `.` operator is used to concatenate strings in C++.

7. What is the size of an empty string in C++?

a) 0

b) 1

c) Undefined

d) 10

- Answer: a) 0

- Explanation: An empty string has a size of 0 as it doesn't contain any characters.

8. How do you access the third character of a string "programming"?

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:

9. What is a pointer in C++?

a) A data type for storing characters

b) A type of loop

c) A variable that stores the address of another variable

d) A container for holding multiple values

- Answer: c) A variable that stores the address of another variable

- Explanation: A pointer is a variable that holds the memory address of another variable.

10. How do you declare a pointer to an integer variable named "numPtr"?

a) int numPtr = &num;

b) pointer numPtr = num;

c) int *numPtr = &num;

d) numPtr = &num;

- Answer: c) int *numPtr = &num;

- 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.

12. What does the address-of operator '&' do in C++?

a) Declares a new variable

b) Creates a new memory address

c) Accesses the value pointed to by a pointer

d) Returns the memory address of a variable

- Answer: d) Returns the memory address of a variable

- Explanation: The address-of operator '&' returns the memory address of a variable.
Pointers and Arrays:

13. How is an array name related to a pointer in C++?

a) Arrays and pointers are unrelated concepts

b) An array name is a pointer to the first element of the array

c) An array name holds the entire array in memory

d) A pointer is an array of characters

- Answer: b) An array name is a pointer to the first element of the array

- 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)

- Explanation: Pointer arithmetic is used to access elements of an array using a pointer.

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;

- Answer: a) ptr = &numbers;

- Explanation: To initialize a pointer to an array, use the address-of operator '&' with the array name.

Dynamic Memory:

16. What is dynamic memory allocation in C++?

a) Allocating memory for arrays only

b) Allocating memory for variables declared at runtime

c) Allocating memory for strings only

d) Allocating memory for static variables

- Answer: b) Allocating memory for variables declared at runtime

- 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;

b) int *num = malloc(sizeof(int));

c) allocate int num;

d) int *

num = new int;

- Answer: d) int *num = new 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);

- Answer: b) delete pointer;

- Explanation: The `delete` operator is used to deallocate memory allocated using the `new` operator.

19. What is a memory leak in C++?

a) Allocating too much memory

b) Using too many pointers

c) Not deallocating memory after its use

d) Using dynamic memory allocation

- Answer: c) Not deallocating memory after its use

- Explanation: A memory leak occurs when memory allocated using dynamic allocation is not deallocated properly.

Data Structures:

20. Which data structure organizes elements in a linear sequence?

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?

a) Linked lists use less memory

b) Linked lists have constant-time access

c) Linked lists can dynamically change size

d) Linked lists can hold only integers

- Answer: c) Linked lists can dynamically change size

- Explanation: Linked lists can grow or shrink dynamically, unlike arrays with fixed sizes.

22. How do you insert an element at the end of a linked list?

a) Insert the element at the beginning

b) Use the `push_back` function

c) Use the `insert` function

d) Linked lists cannot have elements at the end

- Answer: b) Use the `push_back` function

- Explanation: The `push_back` function inserts an element at the end of a linked list.

23. What is a stack data structure?

a) A linear data structure with FIFO access

b) A linear data structure with LIFO access

c) A hierarchical data structure

d) A dynamic array

- Answer: b) A linear data structure with LIFO access

- Explanation: A stack follows the Last-In-First-Out (LIFO) access pattern.

Other Data Types:

24. Which data type is used to store true/false values in C++?

a) char

b) int

c) bool

d) string

- Answer: c) bool

- Explanation: The `bool` data type is used to represent true/false values.


25. How do you declare a variable "isReady" of type `bool` and initialize it with `true`?

a) bool isReady = true;

b) isReady bool = true;

c) bool = isReady(true);

d) isReady = true;

- Answer: a) bool isReady = true;

- Explanation: The correct syntax for declaring and initializing a bool variable is shown in option a.

Classes (I):

26. What is a class in C++?

a) A loop construct

b) A data type that stores numbers

c) A user-defined data type that defines properties and behaviors

d) A type of array

- Answer: c) A user-defined data type that defines properties and behaviors

- Explanation: A class is a blueprint for creating objects with specific properties and behaviors.

27. How do you declare a class named "Car"?

a) class Car;

b) class Car { };

c) create class Car;

d) define Car class;

- Answer: b) class Car { };

- Explanation: The correct syntax for declaring a class is shown in option b.

28. What are member variables in a class?

a) Variables declared outside any function

b) Variables declared within a class but not associated with any object

c) Variables declared inside a function

d) Variables used for arithmetic calculations

- 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) { }

c) Student(name: string, age: int) { }

d) Student(string name, int age) { }

- Answer: d) Student(string name, int age) { }

- Explanation: The correct syntax for defining a constructor is shown in option d.

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.

Friendship and Inheritance:

32. What is the purpose of the "friend" keyword in C++?

a) To declare a class as a friend

b) To define a function within a class

c) To declare a function as a friend

d) To access private members of a class

- Answer: d) To access private members of a class

- Explanation: The "friend" keyword allows a function or another class to access private members of a class.
33. What is inheritance in C++?

a) A mechanism to hide class members

b) A way to define global variables

c) A process of creating new classes from existing classes

d) A type of loop construct

- Answer: c) A process of creating new classes from existing classes

- Explanation: Inheritance allows you to create new classes (derived classes) based on existing classes (base classes).

34. What is a base class in inheritance?

a) The class that inherits from another class

b) The class being inherited from

c) The first class in a program

d) A class with only private members

- Answer: b) The class being inherited from

- Explanation: The base class is the class whose properties and behaviors are inherited by the derived class.

35. How is inheritance represented in C++?

a) Using the '>' symbol

b) Using the '<' symbol

c) Using the '-' symbol

d) Using the ':' symbol

- Answer: d) Using the ':' symbol

- Explanation: In C++, inheritance is represented using the ':' symbol followed by the access mode.

Polymorphism:

36. What is polymorphism in C++?

a) A type of data structure

b) A way to declare multiple variables

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

c) Defining a function within another function

d) Declaring a function outside any class

- 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.

38. How do you achieve runtime polymorphism in C++?

a) Using templates

b) Using the `virtual` keyword and pointers

c) Using inline functions

d) Using static polymorphism

- Answer: b) Using the `virtual` keyword and pointers

- 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:

1. How do you declare an array of integers named "numbers" with 5 elements?

a) int numbers[5];

b) array numbers[5];

c) numbers[5] = int[];

d) numbers = int[5];

- Answer: a) int numbers[5];

- Explanation: The correct syntax for declaring an integer array with 5 elements is shown in option a.

2. How do you access the third element of an array named "scores"?

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].

3. What is the size of an array declared as "double values[10];"?

a) 10

b) 9

c) 11

d) The size cannot be determined

- Answer: a) 10

- Explanation: The array "values" has 10 elements, so its size is 10.

Character Sequences:

4. What data type is used to represent a sequence of characters in C++?

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"?

a) string name = "John";

b) char name = "John";

c) char name[] = "John";

d) name = "John";

- Answer: c) char name[] = "John";

- Explanation: C-style strings are declared using a character array with the null terminator at the end.

6. What is the null terminator character in C++?

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:

7. What is a pointer in C++?

a) A keyword

b) A data type for integers

c) A variable that stores the address of another variable

d) A type of loop construct

- Answer: c) A variable that stores the address of another variable

- Explanation: A pointer is a variable that holds the memory address of another variable.

8. How do you declare a pointer to an integer named "ptr"?

a) int ptr;

b) int *ptr;

c) pointer ptr;

d) *ptr int;

- Answer: b) int *ptr;


- Explanation: The correct syntax for declaring a pointer to an integer is shown in option b.

9. How do you assign the address of an integer variable "num" to a pointer "ptr"?

a) ptr = &num;

b) ptr = num;

c) &ptr = num;

d) *ptr = num;

- Answer: a) ptr = &num;

- Explanation: The address of "num" is assigned to the pointer "ptr" using the address-of operator &.

Pointers / Pointers and Arrays:

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 *.

11. How do you declare a pointer to an array of integers named "arrPtr"?

a) int *arrPtr[];

b) int arrPtr[];

c) int *arrPtr;

d) pointer arrPtr;

- Answer: c) int *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:

13. What is dynamic memory allocation in C++?

a) Allocating memory for global variables

b) Allocating memory during compile-time

c) Allocating memory for local variables

d) Allocating memory during runtime

- Answer: d) Allocating memory during runtime

- 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;

b) int *num = new int;

c) malloc(sizeof(int));

d) allocate int;

- Answer: b) int *num = new 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);

- Answer: b) delete pointer;

- Explanation: The "delete" operator is used to deallocate memory allocated using the "new" operator.

Data Structures:

16. Which data structure organizes elements in a linear sequence?

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?

a) Linked lists use less memory

b) Linked lists have constant-time access

c) Linked lists can dynamically change size

d) Linked lists can hold only integers

- Answer: c) Linked lists can dynamically change size

- Explanation: Linked lists can grow or shrink dynamically, unlike arrays with fixed sizes.

18. How do you insert an element at the end of a linked list?

a) Insert at the first position

b) Insert at the middle position

c) Insert at the last position

d) Linked lists cannot have elements added

- Answer: c) Insert at the last position

- 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.

Other Data Types:

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.

20. What is the value of a boolean expression "true && false"?

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.

21. What is the value of the expression "5 > 3"?

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):

22. What is a class in C++?

a) A type of function

b) A data type for integers

c) A user-defined data type that bundles data and functions

d) A type of loop construct

- Answer: c) A user-defined data type that bundles data and functions

- 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"?

a) class Rectangle { width, height };

b) define Rectangle { width, height };

c) class Rectangle { int width, int height };

d) class Rectangle { public: int width, height; };

- Answer: d) class Rectangle { public: int width, height; };

- Explanation: The correct syntax for defining a class with attributes is shown in option d.

24. What is an object in the context of classes?

a) A function inside a class

b) A variable inside a function

c) An instance of a class that holds data and functions

d) A loop construct inside a class

- Answer: c) An instance of a class that holds data and functions


- Explanation: An object is created based on a class and contains the data and functions defined in the class.

Classes (I) (continued):

25. How do you create an object of the class "Car" named "myCar"?

a) Car myCar();

b) Car myCar;

c) myCar = new Car;

d) myCar = Car();

- Answer: b) Car myCar;

- Explanation: The correct syntax for creating an object is shown in option b.

26. How do you access the "speed" attribute of an object "carObj"?

a) carObj.speed;

b) speed(carObj);

c) carObj.getSpeed();

d) getSpeed(carObj);

- Answer: a) carObj.speed;

- Explanation: Object attributes are accessed using the dot operator.

Classes (II):

27. What is a constructor in a class?

a) A member function with no parameters

b) A member function with the same name as the class

c) A keyword used for defining classes

d) A loop construct in a class

- Answer: b) A member function with the same name as the class

- 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"?

a) constructor Student(name, age) { }

b) Student { name, age }

c) Student(name, age) { }

d) Student(name: string, age: int) { }


- Answer: c) Student(name, age) { }

- Explanation: The correct syntax for defining a constructor with parameters is shown in option c.

29. What is a destructor in a class?

a) A member function with no parameters

b) A member function with the same name as the class

c) A function for performing calculations

d) A keyword used for deallocating memory

- Answer: a) A member function with no parameters

- 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.

Friendship and Inheritance:

30. What is friendship in C++ classes?

a) A type of loop

b) A type of attribute

c) A relationship between classes that allows access to private members

d) A type of pointer

- Answer: c) A relationship between classes that allows access to private members

- Explanation: Friendship allows a class to access private members of another class.

31. How do you declare a friend function named "printInfo" inside a class "Person"?

a) friend printInfo();

b) friend void printInfo();

c) friend Person printInfo();

d) friend void Person::printInfo();

- Answer: b) friend void printInfo();

- Explanation: The correct syntax for declaring a friend function is shown in option b.

32. What is inheritance in C++?

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.

Friendship and Inheritance (continued):

33. What is the base class in inheritance?

a) The derived class

b) The child class

c) The parent class

d) The subclass

- Answer: c) The parent class

- 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"?

a) class Child extends Parent

b) class Child inherits Parent

c) class Child : Parent

d) class Child from Parent

- Answer: c) class Child : Parent

- Explanation: In C++, the colon ":" is used to indicate inheritance.

35. What is a derived class in inheritance?

a)The parent class

b) The base class

c) The subclass

d) The child class

- Answer: c) The subclass

- Explanation: The derived class is the class that inherits attributes and methods from another class.

Polymorphism:

36. What is polymorphism in C++?

a) A type of loop construct

b) A technique where different classes can be treated as instances of the same class through inheritance

c) A type of data structure

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.

37. What is a virtual function in C++?

a) A function that cannot be overridden

b) A function declared in a derived class

c) A function declared with the "virtual" keyword that can be overridden in derived classes

d) A function that cannot be called

- 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();

b) virtual int calculateArea();

c) virtual void calculateArea();

d) virtual Shape calculateArea();

- Answer: c) virtual void calculateArea();

- Explanation: The correct syntax for declaring a virtual function is shown in option c.

Polymorphism (continued):

39. What is function overriding in C++?

a) A technique for hiding private members

b) A technique for modifying base class attributes

c) A technique for providing a new implementation for a virtual function in a derived class

d) A technique for accessing static members

- 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

d) none of the above

- Answer: b) base
- Explanation: The "base" keyword is used to call the overridden function from the base class within a derived class.

Other Data Types (continued):

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.

42. What is the value of the expression "5.0 / 2"?

a) 2.5

b) 2

c) 2.0

d) 2.2

- Answer: a) 2.5

- Explanation: Floating-point division results in a floating-point value, so the result is 2.5.

Classes (II) (continued):

43. What is method overloading in C++?

a) A technique for changing a class's attributes

b) A technique for providing multiple implementations of a function with the same name in a class

c) A technique for changing a class's methods

d) A technique for hiding private members

- 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"?

a) int calculate(int a, int b);

b) void calculate(int a, int b);

c) calculate(int a, int b);

d) overload calculate(int a, int b);


- Answer: a) int calculate(int a, int b);

- Explanation: To overload a function, you provide multiple definitions with different parameter lists.

Friendship and Inheritance (continued):

45. What is multiple inheritance in C++?

a) A relationship where a class inherits from two parent classes

b) A relationship where a class inherits from multiple child classes

c) A technique for changing attribute values in multiple classes

d) A technique for hiding private members in multiple classes

- Answer: a) A relationship where a class inherits from two parent classes

- 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?

a) The compiler automatically resolves it

b) By using the "ambiguity" keyword

c) By renaming one of the functions

d) By avoiding multiple inheritance

- Answer: c) By renaming one of the functions

- Explanation: To resolve ambiguity, you can rename one of the functions in the derived class.

Polymorphism (continued):

47. What is function overloading in C++?

a) A technique for changing attribute values

b) A technique for providing multiple implementations of a function with the same name in a class

c) A technique for overriding functions in a derived class

d) A technique for hiding private members

- 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?

a) Complex operator+(Complex num1, Complex num2);

b) void operator+(Complex num1, Complex num2);

c) operator+(Complex num1, Complex num2);


d) overload operator+(Complex num1, Complex num2);

- Answer: a) Complex operator+(Complex num1, Complex num2);

- Explanation: To overload the "+" operator, you provide a function named "operator+" that takes two Complex objects
as parameters.

Friendship and Inheritance (continued):

49. What is the diamond problem in multiple inheritance?

a) A technique for resolving ambiguity

b) A technique for dealing with function overloads

c) A situation where a derived class inherits from two classes that share a common base class

d) A technique for avoiding inheritance

- 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.

50. How do you achieve "is-a" relationship using inheritance?

a) By using friendship

b) By using multiple inheritance

c) By creating an object

d) By using a base class and a derived class

- Answer: d) By using a base class and a derived class

- Explanation: Inheritance allows you to create a derived class that "is-a" type of the base class.

You might also like