C++ Programming quiz by azka.xdlabin QuizMarch 8, 2025 C++ Programming quiz 1 / 100How can you stop an infinite loop in C++? Using continue Using break Using exit() Both B and C 2 / 100Which data type is used to store a single character in C++? char string character text 3 / 100How do you declare an array of integers with 5 elements in C++? int arr; int arr[5]; array arr; int arr[]; 4 / 100What 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. 5 / 100Which header file is required to use the string class in C++? 6 / 100How 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; ); 7 / 100What is the result of 5 / 2 in C++ (integer division)? 2.5 2 3 5.2 8 / 100What 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 9 / 100What is the default access specifier for members of a class in C++? public private protected default 10 / 100Which of the following is a logical operator in C? && || ! All of the above 11 / 100Which type of constructor does NOT take any parameters? Default constructor Parameterized constructor Copy constructor Destructor 12 / 100What 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 13 / 100What 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 14 / 100What 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 15 / 100If 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 16 / 100Which of the following is the assignment operator in C? == = += != 17 / 100What 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 18 / 100What 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 19 / 100How do you find the size of a string in C++? str.size() str.length() Both A and B strlen(str) 20 / 100What 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 21 / 100Which loop is best used when the number of iterations is known? for loop while loop do-while loop None of the above 22 / 100What is the correct syntax for an if statement in C++? if condition then {} if (condition) {} if {condition} if [condition] {} 23 / 100Which 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. 24 / 100What is the index of the last element in an array of size 7? 7 6 8 0 25 / 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] = {} 26 / 100Which operator is used for the logical AND operation in C++? & && and Both B and C 27 / 100What 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 28 / 100What 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 29 / 100What 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*); 30 / 100Which access specifier is commonly used to implement encapsulation in C++? public private protected friend 31 / 100What 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); 32 / 100How do you create an object of a class named Student? Student(); Student obj; Student = obj; class Student obj; 33 / 100Which access specifier allows members of a class to be accessible only within the same class? public protected private friend 34 / 100What 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 35 / 100Which of the following is the correct syntax for defining a function in C++? void myFunction { } void myFunction(); void myFunction() { } myFunction() void { } 36 / 100What 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++ 37 / 100How do you access the members of a structure variable in C++? structVariable->member structVariable.member structVariable:member structVariable->member() 38 / 100What 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 39 / 100Which of the following is used to take input in C++? cin scanf input getchar 40 / 100What 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 41 / 100What 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 42 / 100Which of the following correctly initializes a C++ string? string str = "Hello"; string str("Hello"); string str{"Hello"}; All of the above 43 / 100What 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 44 / 100Which OOP principle allows one class to inherit from another? Polymorphism Encapsulation Inheritance Abstraction 45 / 100What 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); 46 / 100What 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; 47 / 100What 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 48 / 100What happens if you access an array index that is out of bounds? Compilation error Runtime error Undefined behavior The program stops execution immediately 49 / 100Which 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. 50 / 100What are the three types of access specifiers in C++? public, private, default private, protected, secure public, private, protected public, default, protected 51 / 100What 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 52 / 100What 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 53 / 100What is the return type of a function that does not return any value? void int null none 54 / 100What 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 55 / 100How many times is a constructor called when creating 3 objects of a class? 1 2 3 0 56 / 100What type of operator is ++ in C++? Arithmetic operator Logical operator Increment operator Relational operator 57 / 100What is a reference variable in C++? A pointer variable An alias for an existing variable A dynamic memory variable A constant variable 58 / 100What 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 59 / 100Which function is used to extract a substring in C++? str.part() str.extract() str.substr() str.sub() 60 / 100Which 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; 61 / 100How do you find the size of a string in C++? str.size() str.length() Both A and B strlen(str) 62 / 100What 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. 63 / 100Which of the following is a correct variable declaration in C++? int 2num; float number; double& value = 5; char name = "A"; 64 / 100Which of the following is NOT a pillar of OOP? Encapsulation Inheritance Compilation Polymorphism 65 / 100What 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 66 / 100What 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 67 / 100What 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); 68 / 100How 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() 69 / 100How 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 70 / 100Which keyword is used to define a constant variable in C++? const constant final let 71 / 100What 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 / 100Which of the following is a floating-point data type in C++? int char bool float 73 / 100What 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 74 / 100Which of the following is NOT a valid variable name in C++? my_var _myVar 2ndVar var123 75 / 100What is the default value of an uninitialized integer array element in C++? 0 -1 Garbage value NULL 76 / 100What 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 77 / 100How can encapsulation be violated in C++? By using protected data members By using friend functions By using private data members Encapsulation cannot be violated 78 / 100How do you deallocate memory allocated with new in C++? delete ptr; free(ptr); delete[] ptr; Both A and C (depending on allocation type) 79 / 100Which loop is guaranteed to execute at least once? for loop while loop do-while loop None of the above 80 / 100What is the purpose of a constructor in C++? To destroy objects To initialize objects To free memory To print values 81 / 100How 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 82 / 100Which of the following is NOT a relational operator in C++? == != += < 83 / 100What 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 84 / 100What is the size of a char data type in C++? 1 byte 2 bytes 4 bytes 8 bytes 85 / 100What will be the output of this code? 0 1 2 3 4 0 2 4 0 2 4 6 8 0 1 3 5 86 / 100What is the correct syntax for declaring a reference variable? int &ref = var; int ref = &var; int *ref = var; int &ref == var; 87 / 100What 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 / 100What is the correct syntax for defining a constructor in C++? void Constructor(); Constructor() { } int Constructor() { } class Constructor { } 89 / 100What is the size of an int in most compilers? 2 bytes 4 bytes 8 bytes Compiler-dependent 90 / 100What is recursion in C++? A function calling itself A function overloading another function A function that returns multiple values A function that runs indefinitely 91 / 100Which access specifier allows members to be accessed from outside the class? public private protected static 92 / 100What will be the output of this code? 1 1 2 1 2 3 No output 93 / 100Which of the following is NOT a type of loop in C++? for loop while loop repeat loop do-while loop 94 / 100What 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 95 / 100Which operator is used to allocate memory dynamically in C++? malloc new allocate calloc 96 / 100Which keyword is used to define a structure in C++? class struct define object 97 / 100What will be the output of the following code? 1 2 3 0 1 2 0 1 2 3 0 1 98 / 100What 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 99 / 100Which operator is used to access the memory address of a variable? * & -> % 100 / 100Which keyword is used for multiple conditions in an if statement? elseif elif else if otherwise Your score isThe average score is 88% 0% Restart quiz