chapter-3 . Programming Language and Its Applications
chapter-3 . Programming Language and Its Applications
returns 5.
1. Which of the following is a valid C token?
a) Variable 5. What does the continue statement do in C?
b) Function a) Ends the loop
c) Operator b) Skips the rest of the iteration and moves to the next iteration
d) All of the above c) Exits the program
Answer: d) All of the above d) None of the above
Explanation: C tokens include keywords, identifiers, constants, Answer: b) Skips the rest of the iteration and moves to the next
string literals, and operators. iteration
Explanation: The continue statement skips the remaining loop
2. What is the size of an int variable in C?
body and goes to the next iteration.
a) 2 bytes
b) 4 bytes 6. Which function is used to read a string of text in C?
c) Depends on the system/compiler a) scanf
d) 8 bytes b) printf
Answer: c) Depends on the system/compiler c) gets
Explanation: The size of an int is system-dependent; on most d) puts
32-bit systems, it is 4 bytes. Answer: c) gets
Explanation: gets() reads a string from the input, but it’s unsafe
3. Which operator is used to access the value at the address
and deprecated in favor of fgets().
stored in a pointer?
a) & 7. What is the result of printf("%d", 10 % 3);?
b) * a) 1
c) -> b) 3
d) . c) 10
Answer: b) * d) Error
Explanation: The dereference operator (*) accesses the value Answer: a) 1
stored at a pointer’s address. Explanation: The modulus operator (%) returns the remainder
of the division, which is 1.
4. What will the following code output? ****printf("%d", 5 >
2 ? 5 : 2); 8. Which loop guarantees at least one execution of the loop
a) 2 body?
b) 5 a) for
c) Error b) while
d) None of the above c) do-while
Answer: b) 5 d) None of the above
Answer: c) do-while
Explanation: The do-while loop executes the loop body first 13. Which header file is required to use input/output functions
and then checks the condition. like printf and scanf?
a) stdlib.h
9. How is a string stored in C?
b) stdio.h
a) As an array of characters
c) conio.h
b) As a pointer
d) string.h
c) As an integer
Answer: b) stdio.h
d) As an object
Explanation: The stdio.h header defines standard input/output
Answer: a) As an array of characters
functions like printf and scanf.
Explanation: Strings are stored as null-terminated character
arrays in C. 14. Which format specifier is used for floating-point numbers?
a) %f
10. Which operator is used to concatenate two strings?
b) %d
a) +
c) %c
b) .
d) %s
c) strcat
Answer: a) %f
d) None of the above
Explanation: %f is used for floating-point values in printf or
Answer: c) strcat
scanf.
Explanation: The strcat function is used to concatenate two
strings. 15. What is the purpose of the break statement?
a) Skip to the next iteration
11. What is the default return type of a function in C if not
b) Terminate the loop
specified?
c) Pause the program
a) void
d) None of the above
b) int
Answer: b) Terminate the loop
c) float
Explanation: The break statement terminates the loop and
d) char
transfers control to the next statement.
Answer: b) int
Explanation: If no return type is specified, the default is int. 16. What does a recursive function do?
a) Calls itself
12. Which keyword is used to create a user-defined function?
b) Loops indefinitely
a) def
c) Stops immediately
b) func
d) Executes in reverse order
c) return
Answer: a) Calls itself
d) None of the above
Explanation: A recursive function is one that calls itself within
Answer: d) None of the above
its definition.
Explanation: C functions are declared without any special
keyword; only the return type is specified.
17. What does the sizeof operator return? a) 1
a) The size of the variable b) 2
b) The size of the type in bytes c) 3
c) The address of the variable d) None of the above
d) None of the above Answer: c) 3
Answer: b) The size of the type in bytes Explanation: Array indices start at 0; arr[2] refers to the third
Explanation: The sizeof operator returns the memory size of a element, which is 3.
type or variable in bytes.
22. Which statement is used to stop execution inside a loop?
18. Which data type can store a sequence of characters in C? a) break
a) char b) continue
b) int c) exit
c) float d) None of the above
d) None of the above Answer: a) break
Answer: a) char Explanation: The break statement exits the loop immediately.
Explanation: The char type can hold a sequence of characters
23. Which of the following is not a looping structure in C?
in the form of a string.
a) for
19. What is the purpose of the return statement in a function? b) while
a) End the program c) repeat
b) Pass control back to the calling function d) do-while
c) Start a new function Answer: c) repeat
Explanation: The repeat keyword does not exist in C; valid
d) None of the above
loop structures include for, while, and do-while.
Answer: b) Pass control back to the calling function
24. What happens if you omit the break statement in a switch
Explanation: The return statement exits the function and
case?
optionally passes back a value. a) The program exits the switch block.
20. What is the index of the first element in a C array? b) The next case is executed.
a) 0 c) Compilation error.
d) None of the above.
b) 1
Answer: b) The next case is executed.
c) -1
Explanation: Without break, the program continues executing
d) None of the above the subsequent cases (fall-through).
Answer: a) 0 25. Which function is used to calculate the length of a string in
Explanation: C arrays are zero-indexed, so the first element has C?
an index of 0. a) length()
b) size()
21. What is the value of arr[2] for the array declaration int
c) strlen()
arr[3] = {1, 2, 3};?
d) None of the above 30. Which function terminates a C program immediately?
Answer: c) strlen() a) exit()
Explanation: The strlen function in <string.h> returns the b) terminate()
number of characters in a string, excluding the null terminator. c) close()
26. What is the purpose of the getchar() function? d) None of the above
a) To read a string of characters Answer: a) exit()
b) To read a single character Explanation: The exit() function terminates the program and
c) To write a character can return a value to the operating system.
d) None of the above
Answer: b) To read a single character
Explanation: The getchar() function reads one character
from the standard input (keyboard).
27. Which of the following is a correct way to declare an array
in C?
a) int arr[5];
b) array int arr[5];
c) int arr = [5];
d) None of the above
Answer: a) int arr[5];
Explanation: This is the proper syntax for declaring an array of
integers in C.
28. What will the output be for the following code?
char str[] = "Hello"; printf("%c", str[1]);
a) H
b) e
c) l
d) o
Answer: b) e
Explanation: str[1] refers to the second character in the string
"Hello," which is 'e'.
29. What is the use of #include<stdio.h>?
a) To include standard input/output functions
b) To include string manipulation functions
c) To include math functions
d) None of the above
Answer: a) To include standard input/output functions
Explanation: #include<stdio.h> enables the use of functions
like printf and scanf.
Pointer, Structure and data files in C Programming d) None of the above
Answer: a) Accesses members of a structure via a pointer
1. What is the output of the following code? Explanation: The -> operator is used to access members of a
int a = 5; structure through a pointer.
int *p = &a; 5. What is the output of the following code?
printf("%d", *p); struct point { int x, y; };
struct point p = {10, 20};
a) 5 printf("%d", p.y);
b) Address of a
c) Compilation error a) 10
d) Garbage value b) 20
Answer: a) 5 c) Compilation error
Explanation: Dereferencing the pointer p gives the value of a. d) Undefined behavior
Answer: b) 20
2. What happens when you increment a pointer in C? Explanation: The p.y accesses the y member of the structure.
a) It increments by 1 byte
b) It increments by the size of the data type it points to 6. Which of the following correctly declares a structure in C?
c) It moves to the next memory block a) struct data {int x; float y;};
d) Both b and c b) structure data {int x; float y;};
Answer: d) Both b and c c) data {int x; float y;};
Explanation: Pointer arithmetic increments the pointer by the d) struct data (int x, float y);
size of the data type. Answer: a) struct data {int x; float y;};
3. What is the output of the following code? Explanation: Structures are declared with the struct keyword.
int arr[] = {10, 20, 30};
int *p = arr; 7. What is the size of a union with an integer (4 bytes) and a
printf("%d", *(p + 1)); char array (10 bytes)?
a) 4 bytes
a) 10 b) 10 bytes
b) 20 c) 14 bytes
c) 30 d) Depends on the compiler
d) Compilation error Answer: b) 10 bytes
Answer: b) 20 Explanation: A union’s size is equal to the size of its largest
Explanation: Pointer arithmetic accesses the second element of member.
the array.
8. What is the correct syntax to access the address of a
structure's member?
4. What does the -> operator do in C?
a) &struct_name.member
a) Accesses members of a structure via a pointer
b) struct_name->member
b) Declares a pointer
c) &(struct_name.member)
c) Performs multiplication
d) struct_name.member_address() Explanation: "w" mode truncates the file and positions the
Answer: c) &(struct_name.member) pointer at the beginning.
Explanation: The address of a structure member is accessed 13. What is the output of the following code?
using &.
c
9. Which of the following is true about structures and unions Copy code
in C? FILE *fp = fopen("data.txt", "r");
a) Structures save more memory than unions if (fp == NULL)
printf("Error");
b) Unions share memory among members else
c) Both structures and unions allocate equal memory printf("Success");
d) Unions allocate more memory than structures
Answer: b) Unions share memory among members a) Error
Explanation: In unions, all members share the same memory b) Success
location. c) Compilation error
10. Which of the following opens a file for reading and writing d) Undefined behavior
from the beginning? Answer: a) Error
a) "r" Explanation: If the file data.txt does not exist, fopen returns
b) "w" NULL.
c) "r+"
d) "w+" 14. Which function moves the file pointer to a specific position?
Answer: c) "r+" a) fseek()
Explanation: The "r+" mode allows reading and writing b) ftell()
without truncating the file. c) rewind()
11. Which function is used to write formatted data to a file? d) move()
a) fwrite() Answer: a) fseek()
b) fprintf() Explanation: The fseek() function adjusts the file pointer to a
c) fscanf() specified position.
d) fputc() 15. What does the ftell() function return?
Answer: b) fprintf() a) The current position of the file pointer
Explanation: fprintf() is used for writing formatted data to a b) The size of the file
file. c) The address of the file
12. What is the default file pointer position when opening a file d) The name of the file
in "w" mode? Answer: a) The current position of the file pointer
a) Beginning of the file Explanation: ftell() provides the current position of the file
b) End of the file pointer.
c) Undefined 16. Which function resets the file pointer to the beginning?
d) Middle of the file a) rewind()
Answer: a) Beginning of the file b) fseek()
c) reset() 21. What does the sizeof() operator return for a structure?
d) start() a) The sum of all member sizes
Answer: a) rewind() b) The size of the largest member
Explanation: The rewind() function moves the file pointer to c) The memory allocated to the structure (including padding)
the beginning. d) None of the above
Answer: c) The memory allocated to the structure (including
17. Which file opening mode is used for appending data?
padding)
a) "r"
Explanation: sizeof() considers padding added for
b) "w"
alignment.
c) "a"
d) "r+" 22. Which is not a valid file mode in C?
Answer: c) "a" a) "r+"
Explanation: "a" mode opens a file for appending at the end b) "a+"
without truncating it. c) "x"
d) "rb"
18. What does the fclose() function do?
Answer: c) "x"
a) Opens a file
Explanation: "x" is not a valid file mode in standard C.
b) Deletes a file
c) Closes a file and frees resources 23. What is the difference between fputc() and fputs()?
d) Writes data to a file a) fputc() writes a single character, fputs() writes a string
Answer: c) Closes a file and frees resources b) fputs() writes a character, fputc() writes a string
Explanation: fclose() is used to close a file properly. c) Both write single characters
d) Both write strings
19. Which keyword is used to define a union in C?
Answer: a) fputc() writes a single character, fputs() writes
a) struct
a string
b) class
Explanation: fputc() is for characters; fputs() is for strings.
c) union
d) type 24. What is the output of the following code?
Answer: c) union union data { int x; char y; };
Explanation: The union keyword is used to define unions. union data u;
u.x = 65;
20. How can you read a block of data from a file in C? printf("%c", u.y);
a) fread()
b) fscanf() a) A
c) fgetc() b) Undefined behavior
d) fputs() c) Compilation error
Answer: a) fread() d) 65
Explanation: The fread() function reads a block of binary Answer: a) A
data from a file. Explanation: The union shares memory, and interpreting 65 as
a char gives A.
25. Which of the following is true for pointers in C? 30. Which function checks for the end of a file?
a) A pointer can point to another pointer a) feof()
b) A pointer can be null b) fseek()
c) A pointer must be initialized before use c) fclose()
d) All of the above d) eof()
Answer: d) All of the above Answer: a) feof()
Explanation: All statements are true for pointers. Explanation: feof() checks if the end of a file has been
26. What is the value of a null pointer? reached.
a) 0
b) Undefined
c) Address of the variable
d) None of the above
Answer: a) 0
Explanation: A null pointer is explicitly assigned the value 0.
27. What does the typedef keyword do?
a) Defines a new data type
b) Renames an existing data type
c) Creates a pointer
d) Allocates memory dynamically
Answer: b) Renames an existing data type
Explanation: typedef provides an alias for an existing type.
28. Which function is used to remove a file?
a) delete()
b) remove()
c) fclose()
d) unlink()
Answer: b) remove()
Explanation: The remove() function deletes a file.
29. How is a pointer to a structure defined?
a) struct point *p;
b) struct *point p;
c) *struct point p;
d) struct p *point;
Answer: a) struct point *p;
Explanation: A pointer to a structure is declared using the *
operator.
C++ language construct with object and class • What is the purpose of a constructor in C++?
a) To allocate memory
• What is the purpose of the namespace keyword in C++? b) To initialize an object
a) To define a class c) To destroy an object
b) To avoid naming conflicts d) To inherit a class
c) To create an object Answer: b) To initialize an object
d) To define inheritance Explanation: A constructor initializes an object when it is created.
Answer: b) To avoid naming conflicts
Explanation: Namespaces group entities like classes and functions • What is the correct syntax for defining a class in C++?
under a unique name to prevent name clashes. a) class MyClass { int x; };
b) class MyClass(int x);
• Which of the following is a feature of function overloading? c) MyClass class { int x; };
a) Same function name with different argument types d) MyClass { class int x; };
b) Same function name with the same argument types Answer: a) class MyClass { int x; };
c) Different function names with the same functionality Explanation: Classes are defined using the class keyword followed
d) None of the above by the class name.
Answer: a) Same function name with different argument types
Explanation: Function overloading allows multiple functions with the • What is a default argument in C++?
same name but different parameters. a) An argument that must be provided
b) An argument with a default value if not provided
• What does the inline keyword in C++ do? c) An argument that is always passed by reference
a) Makes a function global d) An argument that cannot be overridden
b) Suggests the compiler to expand the function at the call site Answer: b) An argument with a default value if not provided
c) Prevents a function from being overridden Explanation: Default arguments have predefined values used when no
d) Forces dynamic binding argument is passed.
Answer: b) Suggests the compiler to expand the function at the call
site • Which of the following is used for pass-by-reference in C++?
Explanation: inline functions replace their calls with the function a) *
body to reduce overhead. b) &
c) &&
• Which of the following correctly creates an object in C++? d) **
a) class_name obj; Answer: b) &
b) class obj; Explanation: The & symbol is used to pass arguments by reference.
c) obj = class_name;
d) class_name = obj; • What is the purpose of the this pointer in C++?
Answer: a) class_name obj; a) Points to the base class
Explanation: Objects are instantiated using the syntax class_name b) Refers to the calling object
obj;. c) Refers to a global variable
d) None of the above • What is the return type of a constructor?
Answer: b) Refers to the calling object a) void
Explanation: The this pointer refers to the current object in a class. b) int
c) Depends on the class
• What is function overloading in C++? d) None of the above
a) Using multiple classes in one function Answer: d) None of the above
b) Defining multiple functions with the same name but different Explanation: Constructors have no return type.
parameters
c) Creating multiple copies of a function • Which operator is overloaded for object assignment in C++?
d) None of the above a) +
Answer: b) Defining multiple functions with the same name but b) =
different parameters c) *
Explanation: Function overloading is a feature of polymorphism. d) ->
Answer: b) =
• What is the correct way to access a static member of a class? Explanation: The = operator is overloaded for object assignment.
a) Using the class name
b) Using an object of the class • Which of the following can be a friend of a class?
c) Using a pointer to the class a) Another class
d) None of the above b) A function
Answer: a) Using the class name c) Both a and b
Explanation: Static members are accessed using the class name. d) None of the above
Answer: c) Both a and b
• What is the purpose of a destructor in C++? Explanation: Friend functions and classes can access private members
a) To free memory of a class.
b) To initialize variables
c) To destroy the object • What is the default access specifier for members of a class?
d) Both a and c a) Public
Answer: d) Both a and c b) Protected
Explanation: Destructors clean up when an object goes out of scope. c) Private
d) None
• What is a copy constructor? Answer: c) Private
a) A constructor that copies an object from another Explanation: Class members are private by default.
b) A constructor that initializes a new object
c) A constructor that destroys an object • What is the scope resolution operator in C++?
d) A constructor that creates a default object a) :
Answer: a) A constructor that copies an object from another b) ->
Explanation: Copy constructors initialize an object using another c) ::
object. d) .
Answer: c) :: • What is the difference between class and struct in C++?
Explanation: The :: operator is used to define functions outside a a) Classes are always public, structs are private
class or access namespace members. b) Classes have private members by default, structs have public
members
• How do you dynamically allocate memory for an object in C++? c) Structs cannot have functions, but classes can
a) new d) None of the above
b) malloc() Answer: b) Classes have private members by default, structs have
c) calloc() public members
d) alloc() Explanation: This is the key distinction between class and struct.
Answer: a) new
Explanation: The new operator dynamically allocates memory for • What is the role of access specifiers in C++?
objects. a) To define data types
b) To control access to class members
• What happens if a class has a private constructor? c) To allocate memory dynamically
a) The class cannot be instantiated directly d) To handle exceptions
b) The class is an abstract class Answer: b) To control access to class members
c) The class must have a destructor Explanation: Access specifiers define how class members can be
d) The class cannot inherit from another class accessed.
Answer: a) The class cannot be instantiated directly
Explanation: Private constructors restrict object creation. • What is dynamic memory allocation?
a) Allocating memory during runtime
• What is function overriding? b) Allocating memory during compile time
a) Defining a function multiple times in the same scope c) Fixed memory allocation
b) Defining a base class function in the derived class d) None of the above
c) Calling a function from a base class Answer: a) Allocating memory during runtime
d) None of the above Explanation: Memory is allocated at runtime using new or malloc().
Answer: b) Defining a base class function in the derived class
Explanation: Function overriding redefines a base class function in the • What is a friend function?
derived class. a) A function that can access private members of a class
b) A function that is private to a class
• What does the virtual keyword indicate in C++? c) A function declared outside the class but cannot access private
a) The function cannot be overridden members
b) The function supports dynamic binding d) None of the above
c) The function is called directly from the class Answer: a) A function that can access private members of a class
d) The function is private Explanation: Friend functions are declared with the friend keyword.
Answer: b) The function supports dynamic binding
Explanation: virtual allows runtime polymorphism. • What does the delete operator do in C++?
a) Deletes the class definition
b) Frees dynamically allocated memory d) Compilation
c) Creates an object Answer: d) Compilation
d) Deletes a constructor Explanation: Compilation is a feature of programming in general, not
Answer: b) Frees dynamically allocated memory specific to OOP.
Explanation: The delete operator deallocates memory.
• What is encapsulation in OOP?
• What is an abstract class? a) Binding data and functions together
a) A class with no members b) Inheritance from base class
b) A class with at least one pure virtual function c) Hiding details of data storage
c) A class that cannot be inherited d) None of the above
d) None of the above Answer: a) Binding data and functions together
Answer: b) A class with at least one pure virtual function Explanation: Encapsulation combines data and methods into a single
Explanation: Abstract classes serve as base classes for inheritance. unit.
• Which operator cannot be overloaded? • Which of the following best defines polymorphism?
a) + a) Many forms of inheritance
b) = b) Ability to take multiple forms
c) :: c) Only static binding
d) * d) None of the above
Answer: c) :: Answer: b) Ability to take multiple forms
Explanation: The scope resolution operator cannot be overloaded. Explanation: Polymorphism allows objects to act as instances of their
parent class.
• What is a constant member function?
a) A function that does not change member variables • What is a single inheritance?
b) A function that cannot be called a) A class inheriting from multiple classes
c) A function with a constant name b) A class inheriting from one base class
d) None of the above c) A class inheriting from no class
Answer: a) A function that does not change member variables d) A class inheriting from itself
Explanation: Declaring a function as const ensures it does not modify Answer: b) A class inheriting from one base class
the object. Explanation: Single inheritance involves one derived class inheriting
from one base class.
FEATURES OF OOP
• Which of the following is a type of constructor used for
• Which of the following is not a feature of Object-Oriented inheritance in OOP?
Programming (OOP)? a) Base constructor
a) Encapsulation b) Derived constructor
b) Polymorphism c) Copy constructor
c) Inheritance d) Virtual constructor
Answer: c) Copy constructor • What is an abstract class?
Explanation: Copy constructors are used to create a new object as a a) A class with private members only
copy of an existing object. b) A class that cannot be instantiated
c) A class with no constructor
• Which type of inheritance involves a class inheriting from d) A class that does not support inheritance
multiple base classes? Answer: b) A class that cannot be instantiated
a) Single inheritance Explanation: Abstract classes serve as templates for other classes.
b) Multiple inheritance
c) Multilevel inheritance • What is operator overloading?
d) Hybrid inheritance a) Using multiple operators in one expression
Answer: b) Multiple inheritance b) Defining a new behavior for an existing operator
Explanation: Multiple inheritance involves more than one base class. c) Adding new operators to the language
d) None of the above
• What is a pure virtual function? Answer: b) Defining a new behavior for an existing operator
a) A function without implementation in the base class Explanation: Operator overloading allows operators to work with user-
b) A function that overrides a base function defined types.
c) A function that only exists in the derived class
d) A private function in a base class • Which of the following is a binary operator?
Answer: a) A function without implementation in the base class a) +
Explanation: A pure virtual function must be overridden by derived b) ++
classes. c) &
d) !
• Which of the following describes multilevel inheritance? Answer: a) +
a) A derived class inherits from multiple base classes Explanation: Binary operators operate on two operands.
b) A class inherits from another derived class
c) Multiple classes inherit from a single base class • What is the difference between function overloading and
d) None of the above function overriding?
Answer: b) A class inherits from another derived class a) Overloading occurs in the same class, overriding in derived class
Explanation: Multilevel inheritance involves chaining of inheritance. b) Overloading is runtime, overriding is compile-time
c) Overloading requires inheritance, overriding does not
• What is hybrid inheritance? d) None of the above
a) Combining single and multiple inheritance Answer: a) Overloading occurs in the same class, overriding in
b) Inheriting multiple times from the same base class derived class
c) A class without a base class Explanation: Overloading occurs in one class; overriding occurs
d) None of the above across inheritance.
Answer: a) Combining single and multiple inheritance
Explanation: Hybrid inheritance combines the features of single and • What is constructor overloading?
multiple inheritance. a) Defining multiple constructors in the same class
b) Creating a constructor in a derived class Answer: a) Prevents further inheritance of a class
c) Overriding the base class constructor Explanation: final restricts inheritance of a class or overriding of a
d) None of the above method.
Answer: a) Defining multiple constructors in the same class
Explanation: Constructor overloading allows multiple constructors • Which OOP feature promotes code reuse?
with different arguments. a) Inheritance
b) Encapsulation
• Which inheritance type may lead to a diamond problem? c) Polymorphism
a) Single inheritance d) None of the above
b) Multilevel inheritance Answer: a) Inheritance
c) Multiple inheritance Explanation: Inheritance allows new classes to reuse and extend the
d) Hierarchical inheritance functionality of existing classes.
Answer: c) Multiple inheritance
Explanation: Diamond problem occurs when two base classes of a • What is static polymorphism?
derived class share a common base. a) Achieved through function overloading
b) Achieved through function overriding
• What is the purpose of a virtual destructor? c) Achieved through virtual functions
a) To prevent memory leaks in inheritance d) None of the above
b) To override base class destructor Answer: a) Achieved through function overloading
c) To initialize derived class members Explanation: Static polymorphism is determined at compile time.
d) None of the above
Answer: a) To prevent memory leaks in inheritance • What is runtime polymorphism?
Explanation: Virtual destructors ensure the correct destructor a) Achieved through virtual functions
sequence. b) Achieved through function overloading
c) Achieved through inline functions
• What is the default access specifier in C++ for class members? d) None of the above
a) Private Answer: a) Achieved through virtual functions
b) Public Explanation: Runtime polymorphism is determined at runtime.
c) Protected
d) None • Which of the following is not a type of inheritance in C++?
Answer: a) Private a) Multilevel
Explanation: Class members are private by default. b) Hybrid
c) Sequential
• What does the final keyword do in C++ inheritance? d) Multiple
a) Prevents further inheritance of a class Answer: c) Sequential
b) Makes a class static Explanation: Sequential is not an inheritance type in C++.
c) Prevents creation of objects
d) None of the above
• What is the primary advantage of polymorphism? b) To prevent a function from being overridden
a) Faster compilation c) To declare a private method
b) Code reusability and flexibility d) None of the above
c) Reduces code size Answer: a) To explicitly specify overriding a base class method
d) None of the above Explanation: The override keyword is used to ensure the derived
Answer: b) Code reusability and flexibility method overrides a base class method.
Explanation: Polymorphism allows dynamic behavior and reuse of
code. • What does the explicit keyword do in C++?
a) Prevents implicit conversions for constructors
• What does the protected keyword mean in C++? b) Declares a function as private
a) Members are accessible only within the class c) Prevents inheritance
b) Members are accessible in the class and derived classes d) None of the above
c) Members are accessible everywhere Answer: a) Prevents implicit conversions for constructors
d) None of the above Explanation: explicit avoids unintended implicit constructor calls.
Answer: b) Members are accessible in the class and derived classes
Explanation: Protected members are shared with derived classes but • What is constructor delegation?
not with the outside world. a) A constructor calling another constructor in the same class
b) A constructor calling a destructor
• What is the relationship between a base class and derived class c) A derived class constructor overriding the base class
called? d) None of the above
a) "Has-a" relationship Answer: a) A constructor calling another constructor in the same class
b) "Is-a" relationship Explanation: Constructor delegation simplifies constructor
c) "Uses-a" relationship initialization.
d) None of the above
Answer: b) "Is-a" relationship • Which operator is overloaded by default in classes?
Explanation: A derived class is a type of its base class. a) Assignment operator (=)
b) Addition operator (+)
• What is hybrid inheritance? c) Equality operator (==)
a) Combining multiple and multilevel inheritance d) None of the above
b) Inheriting from the same base class multiple times Answer: a) Assignment operator (=)
c) Both a and b Explanation: The assignment operator is implicitly overloaded for
d) None of the above classes by default.
Answer: a) Combining multiple and multilevel inheritance
Explanation: Hybrid inheritance is a mix of multiple and multilevel
inheritance.
• What is the function of ios::trunc? • Which standard library is used for generic programming?
a) Opens a file for appending a) Standard Template Library (STL)
b) Truncates existing file content b) C++ Standard Library
c) Reads the file from the beginning c) Boost Library
d) Converts file content to binary d) None of the above
Answer: b) Truncates existing file content Answer: a) Standard Template Library (STL)
Explanation: The ios::trunc mode erases all previous data in the Explanation: STL provides templates for containers, iterators, and
file. algorithms.
• What is the purpose of getline() in file handling? • What is the purpose of try, catch, and throw in C++?
a) To read a string line by line from a file a) Memory allocation
b) To write a string line by line to a file b) File handling
c) To terminate the program c) Exception handling
d) None of the above d) None of the above
Answer: a) To read a string line by line from a file Answer: c) Exception handling
Explanation: The getline() function extracts a line of text from a Explanation: These keywords handle runtime errors effectively.
file.
• What is a class template?
Pure Virtual Function and file Handeling a) A class that can handle multiple data types
b) A predefined library class
• What is a function template in C++? c) A class with no data members
a) A function that can work with multiple data types d) A class with constant members only
b) A function with default arguments Answer: a) A class that can handle multiple data types
c) A precompiled function Explanation: Class templates allow creating classes that work with any
d) A function that can only work with integers data type.
Answer: a) A function that can work with multiple data types
Explanation: Function templates allow functions to operate on • What is rethrowing an exception?
different types without rewriting. a) Catching and throwing the same exception again
b) Throwing a new exception in the catch block
• What keyword is used to define a template in C++? c) Overriding an exception
a) class d) None of the above
b) template Answer: a) Catching and throwing the same exception again
Explanation: Rethrowing allows passing the exception to another 11. What is the use of throw in C++?
handler. a) To define a template
b) To raise an exception
• Which function is used to catch all exceptions? c) To terminate the program
a) catch(...) d) To handle exceptions
b) try_all() Answer: b) To raise an exception
c) throw_all() Explanation: The throw keyword is used to explicitly raise an
d) default_catch() exception.
Answer: a) catch(...)
Explanation: catch(...) is a generic catch block for any exception. 12. What happens if an exception is not caught in C++?
a) The program crashes immediately
• What does std::exception represent in C++? b) The program executes the default exception handler
a) A base class for all exceptions c) The program returns to main
b) A derived class for runtime errors d) The exception is ignored
c) A class for syntax errors Answer: b) The program executes the default exception
d) None of the above handler
Answer: a) A base class for all exceptions Explanation: If no catch block handles an exception, the
Explanation: std::exception serves as the base class for exception
program calls std::terminate().
handling.
13. Which container does not allow duplicate keys?
• What is an iterator in STL? a) vector
a) A pointer-like object for traversing containers b) set
b) A special type of loop c) list
c) A function template d) queue
d) A library for exception handling Answer: b) set
Answer: a) A pointer-like object for traversing containers
Explanation: The set container stores unique elements in sorted
Explanation: Iterators enable sequential access to elements in
containers. order.
14. What is the function of std::bad_alloc?
• What is a container in STL? a) Indicates failure of file operations
a) A data structure to store collections of data b) Handles memory allocation errors
b) A type of function c) Handles exceptions related to arrays
c) A class template for exception handling
d) None of the above
d) None of the above
Answer: b) Handles memory allocation errors
Answer: a) A data structure to store collections of data
Explanation: Containers like vector and map are used for generic data Explanation: std::bad_alloc is thrown when memory allocation
storage. using new fails.
15. What is a function object in STL? 19. Which STL container uses a LIFO (Last In, First Out)
a) A pointer to a function principle?
b) A class that overloads the function call operator a) vector
c) A global function b) queue
d) None of the above c) stack
Answer: b) A class that overloads the function call operator d) deque
Explanation: Function objects (functors) behave like functions Answer: c) stack
but are implemented as objects. Explanation: The stack container operates on a LIFO basis.
16. Which of the following is an associative container in STL? 20. What is the role of std::runtime_error in C++?
a) vector a) Indicates file handling errors
b) map b) Handles runtime exceptions
c) deque c) Catches syntax errors
d) stack d) Handles compile-time errors
Answer: b) map Answer: b) Handles runtime exceptions
Explanation: Associative containers like map and set store key- Explanation: std::runtime_error is used for exceptions that
value pairs and allow fast retrieval. occur during the program’s execution.
17. What is the primary purpose of a try block in C++? 21. What is the difference between catch(...) and
a) To define variables catch(Exception& e)?
b) To execute code that may throw exceptions a) catch(...) catches all exceptions, while catch(Exception& e)
c) To catch exceptions catches only specific exceptions
d) To handle memory management b) Both behave the same
Answer: b) To execute code that may throw exceptions c) catch(Exception& e) is more efficient
Explanation: The try block contains code that might raise an d) catch(...) cannot rethrow exceptions
exception. Answer: a) catch(...) catches all exceptions, while
catch(Exception& e) catches only specific exceptions
18. What does throw; (without any argument) do inside a catch
Explanation: catch(...) is a generic catch-all block.
block?
a) Rethrows the caught exception 22. What is the std::bad_cast exception used for?
b) Terminates the program a) Error in memory allocation
c) Ignores the exception b) Error in dynamic typecasting
d) Throws a new exception c) Error in file handling
Answer: a) Rethrows the caught exception d) None of the above
Explanation: It passes the exception to another handler for Answer: b) Error in dynamic typecasting
further processing. Explanation: It is thrown when a bad type conversion is
attempted with dynamic_cast.
23. What is the purpose of the iterator class in STL? a) Compilation error
a) To implement algorithms b) Runtime error
b) To access elements of containers c) The function is not compiled
c) To handle exceptions d) None of the above
d) None of the above Answer: c) The function is not compiled
Answer: b) To access elements of containers Explanation: Templates are compiled only when instantiated.
Explanation: Iterators provide sequential access to container
28. Which container is implemented as a doubly-linked list in
elements.
STL?
24. What does the std::logic_error class represent? a) vector
a) Errors detected by the program logic b) deque
b) Runtime errors c) list
c) Syntax errors d) set
d) Memory allocation errors Answer: c) list
Answer: a) Errors detected by the program logic Explanation: The list container is a doubly-linked list.
Explanation: This class is used for errors caused by invalid
29. Which function is used to specify exception specifications
operations.
for a function?
25. Which STL container allows duplicate keys? a) throw()
a) set b) catch()
b) multiset c) try()
c) map d) exception()
d) priority_queue Answer: a) throw()
Answer: b) multiset Explanation: It specifies the types of exceptions a function
Explanation: The multiset container allows duplicate elements. may throw.
26. What is the function of std::terminate in exception 30. Which STL algorithm is used to sort elements?
handling? a) find()
a) To catch uncaught exceptions b) sort()
b) To end the program if no exception is caught c) reverse()
c) To throw an exception d) swap()
d) None of the above Answer: b) sort()
Answer: b) To end the program if no exception is caught Explanation: The sort() algorithm arranges elements in
Explanation: It is the default behavior when an uncaught ascending or descending order.
exception occurs.
27. What happens if a function template is defined but not
instantiated?
Generic Programming and Exception Handling • What is the Standard Template Library (STL)?
a) A library of C++ templates for data structures and algorithms
• What is a template in C++? b) A collection of global functions
a) A way to write generic functions or classes c) A library for input/output operations
b) A type of loop structure d) None of the above
c) A type of variable Answer: a) A library of C++ templates for data structures and
d) None of the above algorithms
Answer: a) A way to write generic functions or classes Explanation: STL provides data structures, algorithms, and iterators.
Explanation: Templates allow writing code that can work with any
data type. • Which of the following is NOT an STL container?
a) vector
• Which keyword is used to define a function template? b) stack
a) template c) string
b) generic d) queue
c) define Answer: c) string
d) class Explanation: string is not an STL container but a separate class in
Answer: a) template C++.
Explanation: The template keyword is used to define templates in
C++. • Which header file must be included to use STL algorithms?
a) <iostream>
• What is a class template? b) <algorithm>
a) A class containing member functions c) <stl>
b) A generic class that works with any data type d) <containers>
c) A predefined library class Answer: b) <algorithm>
d) None of the above Explanation: <algorithm> provides access to STL algorithms like
Answer: b) A generic class that works with any data type sort, find, etc.
Explanation: Class templates are used to create classes that can
operate with any data type. • What is an iterator in STL?
a) A pointer to an array
• What is function overloading with templates? b) An object to traverse elements in a container
a) Writing multiple templates with the same name c) A loop structure
b) Writing multiple functions with the same name d) None of the above
c) Using templates with overloaded functions Answer: b) An object to traverse elements in a container
d) Using templates without parameters Explanation: Iterators provide sequential access to elements in
Answer: c) Using templates with overloaded functions containers.
Explanation: Function overloading with templates allows multiple
template functions with the same name. • What are the three main constructs of exception handling in
C++?
a) if, else, while d) stack
b) try, catch, throw Answer: b) vector
c) switch, case, break Explanation: Vectors are dynamic arrays that grow or shrink in size as
d) for, do, goto needed.
Answer: b) try, catch, throw
Explanation: Exception handling in C++ involves try, catch, and • What is the default behavior of the terminate() function?
throw. a) Handle an exception
b) End the program execution
• Which exception handler catches all exceptions? c) Retry the exception
a) catch(...) d) Throw a new exception
b) catch(Exception) Answer: b) End the program execution
c) catch(void) Explanation: terminate() is called if an exception is not caught.
d) catch_all()
Answer: a) catch(...) • What is a function template specialization?
Explanation: The catch(...) block catches all types of exceptions. a) Defining a generic function
b) Providing a specific implementation for a particular type
• What is the purpose of rethrowing an exception? c) Writing multiple template functions
a) To terminate the program d) None of the above
b) To pass the exception to another handler Answer: b) Providing a specific implementation for a particular type
c) To log the exception Explanation: Template specialization allows defining custom behavior
d) To catch the exception again for specific data types.
Answer: b) To pass the exception to another handler
Explanation: Rethrowing passes the exception to another handler for • Which STL component is used to manage input/output streams?
further processing. a) vector
b) iostream
• How do you throw an exception with arguments? c) fstream
a) throw Exception(args); d) Both b and c
b) throw args; Answer: d) Both b and c
c) throw(Exception) Explanation: iostream and fstream manage input and output
d) Exception(args); operations in C++.
Answer: a) throw Exception(args);
Explanation: Exceptions can be thrown with arguments to provide • What is the role of catch(std::exception& e)?
additional context. a) Catch syntax errors
b) Catch memory errors
• Which class in STL provides dynamic arrays? c) Catch standard exceptions
a) array d) Catch runtime errors
b) vector Answer: c) Catch standard exceptions
c) deque
Explanation: std::exception is the base class for all standard Explanation: reverse() is used to invert the order of elements in a
exceptions. container.
• What is the correct syntax for a class template? • Which function is used to handle uncaught exceptions?
a) template<typename T> class MyClass { }; a) std::terminate()
b) class template<T> MyClass { }; b) std::unexpected()
c) template<T> class MyClass { }; c) std::set_terminate()
d) template<class T> MyClass { }; d) std::catch_all()
Answer: a) template<typename T> class MyClass { }; Answer: c) std::set_terminate()
Explanation: The correct syntax for defining a class template uses Explanation: std::set_terminate() sets a custom handler for
template<typename T>. uncaught exceptions.
• Which of the following allows handling multiple exceptions? • What is the role of typename in templates?
a) Multiple try blocks a) To declare a constant variable
b) Multiple catch blocks b) To indicate a placeholder for a data type
c) Nested throw statements c) To initialize a template
d) Multiple if conditions d) None of the above
Answer: b) Multiple catch blocks Answer: b) To indicate a placeholder for a data type
Explanation: Multiple catch blocks handle different types of Explanation: typename is used in templates to define a generic type.
exceptions.
• What is the output of STL sort algorithm by default?
• What is the primary purpose of exception handling? a) Descending order
a) To debug programs b) Ascending order
b) To gracefully handle runtime errors c) Random order
c) To manage memory efficiently d) Unsorted elements
d) To optimize code execution Answer: b) Ascending order
Answer: b) To gracefully handle runtime errors Explanation: The sort algorithm sorts elements in ascending order by
Explanation: Exception handling ensures the program continues to run default.
or ends gracefully during errors.
• What does catch(Exception e) do?
• Which STL algorithm reverses the order of elements in a a) Handles all exceptions
container? b) Handles exceptions of type Exception
a) reverse() c) Handles runtime errors only
b) sort() d) None of the above
c) find() Answer: b) Handles exceptions of type Exception
d) swap() Explanation: The catch block only matches exceptions of the specified
Answer: a) reverse() type.
• What is the correct way to define a template function? c) To initialize memory
a) template<typename T> void function(T arg); d) To throw an exception
b) function<typename T>(T arg); Answer: a) To handle memory allocation errors
c) template T function(T arg); Explanation: std::bad_alloc is thrown when memory allocation
d) void function template<T>(T arg); using new fails.
Answer: a) template<typename T> void function(T arg);
Explanation: Templates use template<typename T> followed by the
function signature.