C++ Programming quiz by azka.xdlabin QuizMarch 8, 2025 C++ Programming quiz 1 / 100 Which of the following is NOT a relational operator in C++? == != += < 2 / 100 Which header file is required to use the string class in C++? 3 / 100 What does the & operator do when used with a variable? It returns the value of the variable It returns the address of the variable It multiplies the value of the variable It divides the value of the variable 4 / 100 Which keyword is used to define a structure in C++? class struct define object 5 / 100 Which of the following is a logical operator in C? && || ! All of the above 6 / 100 What is the correct syntax to declare a constant variable in C++? int const x = 10; constant int x = 10; const int x = 10; Both A and C 7 / 100 How do you find the size of a string in C++? str.size() str.length() Both A and B strlen(str) 8 / 100 What is an object in C++? A data type that stores values A function that returns a value An instance of a class A keyword used for memory allocation 9 / 100 What is the best way to access private data members of a class from outside? Declare them as public Use getter and setter functions Use friend functions only Declare them as protected 10 / 100 Which of the following is the correct way to initialize multiple variables in C++? int a, b = 5, c = 10; int a = b = c = 5; int a = 5, b = 10, c; int a = 5, b = 10, c = 15; 11 / 100 What is an object in C++? A function inside a class A data type similar to int and float An instance of a class A function that creates a class 12 / 100 Which of the following is NOT a valid variable name in C++? my_var _myVar 2ndVar var123 13 / 100 What is a reference variable in C++? A pointer variable An alias for an existing variable A dynamic memory variable A constant variable 14 / 100 What is a class in C++? A built-in data type A blueprint for creating objects A function that operates on objects A keyword used for loops 15 / 100 Which type of constructor does NOT take any parameters? Default constructor Parameterized constructor Copy constructor Destructor 16 / 100 What is a destructor in C++? A function that deletes objects manually A function with the same name as the class but prefixed with ~ A function that must be called explicitly A function that initializes objects 17 / 100 What will happen if a class member is declared as public? It can be accessed only inside the class It can be accessed outside the class It cannot be accessed at all It can be accessed only by friend functions 18 / 100 How do you define a constructor in C++? By using the class keyword By defining a function with the same name as the class By creating a function with a return type By using the new keyword 19 / 100 What will happen if a class member is declared as private? It can be accessed from anywhere It can only be accessed within the same class Allows access within the same class and derived classes Makes members accessible only within functions of the same class 20 / 100 What will be the output of this code? 0 1 2 3 4 0 2 4 0 2 4 6 8 0 1 3 5 21 / 100 Which function is used to extract a substring in C++? str.part() str.extract() str.substr() str.sub() 22 / 100 What is recursion in C++? A function calling itself A function overloading another function A function that returns multiple values A function that runs indefinitely 23 / 100 What is the size of a char data type in C++? 1 byte 2 bytes 4 bytes 8 bytes 24 / 100 What is a pointer in C++? A variable that stores an address of another variable A reference to a variable A special type of array A function that returns an address 25 / 100 What will be the output of this code? 1 1 2 1 2 3 No output 26 / 100 What is the correct syntax for an if statement in C++? if condition then {} if (condition) {} if {condition} if [condition] {} 27 / 100 What is a function in C++? A block of code that runs automatically A reusable block of code that performs a specific task A loop that executes repeatedly A data type in C++ 28 / 100 What is the correct way to write a ternary operator in C++? condition ? expression1 : expression2; condition ? expression1, expression2; condition :: expression1 :: expression2; condition if expression1 else expression2; 29 / 100 What will be the output of the following code? 1 2 3 0 1 2 0 1 2 3 0 1 30 / 100 If a derived class uses private inheritance, how will the public members of the base class behave in the derived class? They remain public in the derived class They become private in the derived class They become protected in the derived class They are not inherited at all 31 / 100 Which of the following statements is true about arrays in C++? Arrays can have different data types in the same declaration. Arrays always start from index 1. The size of an array must be a constant expression. Arrays can only store integer values. 32 / 100 What is a parameterized constructor? A constructor that takes arguments A constructor that takes no arguments A constructor that is used to destroy objects A constructor that returns a value 33 / 100 What type of operator is ++ in C++? Arithmetic operator Logical operator Increment operator Relational operator 34 / 100 What is the purpose of a constructor in C++? To destroy objects To initialize objects To free memory To print values 35 / 100 What is function overloading in C++? Calling multiple functions inside another function Declaring multiple functions with the same name but different parameters Using functions with too many lines of code Writing a function that calls itself 36 / 100 Which operator is used to access the memory address of a variable? * & -> % 37 / 100 How do you get the total number of elements in an array arr of size 10? sizeof(arr) / sizeof(arr[0]) sizeof(arr) * sizeof(arr[0]) length(arr) arr.length() 38 / 100 What is the correct syntax for defining a constructor in C++? void Constructor(); Constructor() { } int Constructor() { } class Constructor { } 39 / 100 What is the default access specifier for members of a class in C++? public private protected default 40 / 100 How do you access the members of a structure variable in C++? structVariable->member structVariable.member structVariable:member structVariable->member() 41 / 100 What is a NULL pointer? A pointer that is uninitialized A pointer that points to 0 or nullptr A pointer that points to an invalid memory location A pointer to a function 42 / 100 How do you find the size of a string in C++? str.size() str.length() Both A and B strlen(str) 43 / 100 What is the best way to access private data members of a class from outside? Declare them as public Use getter and setter functions Use friend functions only Declare them as protected 44 / 100 Which of the following is NOT a pillar of OOP? Encapsulation Inheritance Compilation Polymorphism 45 / 100 Which of the following statements about the switch statement is true? switch can only be used with int and char values. Every case must have a break statement. switch statements cannot have a default case. switch is slower than if-else for small numbers of conditions. 46 / 100 What happens if you access an array index that is out of bounds? Compilation error Runtime error Undefined behavior The program stops execution immediately 47 / 100 What is the correct way to pass a structure to a function? void display(struct myStruct s); void display(myStruct s); void display(struct s); Both A and B 48 / 100 How do you define a structure in C++? struct { int a; float b; }; struct myStruct { int a; float b; }; structure myStruct { int a; float b; }; struct myStruct ( int a; float b; ); 49 / 100 What is the default value of an uninitialized integer array element in C++? 0 -1 Garbage value NULL 50 / 100 What is the correct way to pass a variable by reference to a function? void myFunction(int &x); void myFunction(int *x); void myFunction(int x&); void myFunction(int x*); 51 / 100 Which operator is used for the logical AND operation in C++? & && and Both B and C 52 / 100 What is a class in C++? A variable that stores multiple values A blueprint for creating objects A function that performs an operation A keyword used to create loops 53 / 100 What are the three types of access specifiers in C++? public, private, default private, protected, secure public, private, protected public, default, protected 54 / 100 How many times is a constructor called when creating 3 objects of a class? 1 2 3 0 55 / 100 What is the key characteristic of a constructor? It has a return type It has the same name as the class It must take at least one argument It must be called explicitly 56 / 100 What is the correct syntax for a function with a default parameter? void myFunction(int x = 10); void myFunction(x = 10); void myFunction(int x == 10); void myFunction(int = 10 x); 57 / 100 Which access specifier allows members to be accessed from outside the class? public private protected static 58 / 100 What happens when you try to change the reference to refer to another variable? It successfully changes the reference. Compilation error. It creates a new reference. The program crashes. 59 / 100 Which keyword is used for multiple conditions in an if statement? elseif elif else if otherwise 60 / 100 Which of the following is a floating-point data type in C++? int char bool float 61 / 100 What is function overloading? Defining multiple functions with the same name but different parameters Calling multiple functions inside another function Using a function multiple times in a loop Declaring a function inside another function 62 / 100 What does str.empty() return? true if the string is empty false if the string is empty Length of the string First character of the string 63 / 100 Which of the following correctly initializes a C++ string? string str = "Hello"; string str("Hello"); string str{"Hello"}; All of the above 64 / 100 What is a constructor in C++? A function that initializes objects of a class A function that destroys objects of a class A function that must return an integer A function that can be called multiple times manually 65 / 100 Which of the following is the correct syntax for defining a function in C++? void myFunction { } void myFunction(); void myFunction() { } myFunction() void { } 66 / 100 How can encapsulation be violated in C++? By using protected data members By using friend functions By using private data members Encapsulation cannot be violated 67 / 100 What is the return type of a function that does not return any value? void int null none 68 / 100 What is the correct syntax for declaring a reference variable? int &ref = var; int ref = &var; int *ref = var; int &ref == var; 69 / 100 Which of the following is the assignment operator in C? == = += != 70 / 100 What is an advantage of using classes in C++? They make code execution faster They help in organizing code using OOP principles They allow programs to run without main functions They replace all built-in data types 71 / 100 What will happen if no constructor is defined in a C++ class? The program will not compile The object cannot be created A default constructor will be provided by the compiler The destructor will be called automatically 72 / 100 What is the purpose of a copy constructor? To copy objects using assignment (=) To create a new object by copying an existing object To delete an object To initialize static variables 73 / 100 Which of the following is a correct variable declaration in C++? int 2num; float number; double& value = 5; char name = "A"; 74 / 100 Which access specifier is commonly used to implement encapsulation in C++? public private protected friend 75 / 100 Which data type is used to store a single character in C++? char string character text 76 / 100 What is the difference between pointers and references in C++? A reference must be initialized when declared, but a pointer can be null A reference can change the object it refers to, but a pointer cannot. References use more memory than pointers. There is no difference. 77 / 100 Which of the following is used to take input in C++? cin scanf input getchar 78 / 100 How do you create an object of a class named Student? Student(); Student obj; Student = obj; class Student obj; 79 / 100 Which of the following is NOT a type of loop in C++? for loop while loop repeat loop do-while loop 80 / 100 Which operator is used to allocate memory dynamically in C++? malloc new allocate calloc 81 / 100 How do you initialize all elements of an integer array arr[5] to 0? int arr[5] = {0}; int arr[5] = 0; int arr = {0, 0, 0, 0, 0}; int arr[5] = {} 82 / 100 What is the index of the last element in an array of size 7? 7 6 8 0 83 / 100 What does the protected access specifier do? Makes members accessible from anywhere Prevents members from being accessed at all Allows access within the same class and derived classes Makes members accessible only within functions of the same class 84 / 100 What is the correct way to read an entire line of input as a string? cin >> str; getline(cin, str); cin.getline(str); readline(cin, str); 85 / 100 What is encapsulation in OOP? Hiding the data members of a class Inheriting properties from another class Overloading functions in a class Using multiple objects in a single class 86 / 100 What happens if you dereference an uninitialized pointer? The program will compile successfully The program may crash or produce undefined behavior The program will print a garbage value The pointer will automatically point to NULL 87 / 100 What is the range of values for an int data type in most 32-bit systems? -128 to 127 0 to 255 -32,768 to 32,767 -2,147,483,648 to 2,147,483,647 88 / 100 Which keyword is used to define a constant variable in C++? const constant final let 89 / 100 What is the result of 5 / 2 in C++ (integer division)? 2.5 2 3 5.2 90 / 100 How can you stop an infinite loop in C++? Using continue Using break Using exit() Both B and C 91 / 100 Which loop is guaranteed to execute at least once? for loop while loop do-while loop None of the above 92 / 100 Which loop is best used when the number of iterations is known? for loop while loop do-while loop None of the above 93 / 100 What is the correct way to read an entire line of input as a string? cin >> str; getline(cin, str); cin.getline(str); readline(cin, str); 94 / 100 What is the size of an int in most compilers? 2 bytes 4 bytes 8 bytes Compiler-dependent 95 / 100 What is an abstract class in C++? A class that cannot have objects A class with at least one pure virtual function A class with only private members A class that contains no functions 96 / 100 Which access specifier allows members of a class to be accessible only within the same class? public protected private friend 97 / 100 How do you dynamically allocate memory for an integer using pointers? int* ptr = new int; int ptr = new int; int* ptr = malloc(sizeof(int)); Both A and C 98 / 100 How do you deallocate memory allocated with new in C++? delete ptr; free(ptr); delete[] ptr; Both A and C (depending on allocation type) 99 / 100 How do you declare an array of integers with 5 elements in C++? int arr; int arr[5]; array arr; int arr[]; 100 / 100 Which OOP principle allows one class to inherit from another? Polymorphism Encapsulation Inheritance Abstraction Your score isThe average score is 88% 0% Restart quiz