1In C++, which operator is used to access the memory address of a variable?
A.
B.
C.
D.$.$
Correct Answer:
Explanation:
The address-of operator returns the memory address of the operand.
Incorrect! Try again.
2What is the primary difference between malloc() in C and new in C++?
A.Neither calls the constructor.
B.new calls the constructor, malloc() does not.
C.malloc() calls the constructor, new does not.
D.Both call the constructor.
Correct Answer: new calls the constructor, malloc() does not.
Explanation:
new is an operator that allocates memory and initializes the object by calling its constructor, whereas malloc() is a library function that only allocates raw memory bytes.
Incorrect! Try again.
3In the context of Parameter Passing, what happens in Pass by Value?
A.An alias to the actual parameter is created.
B.The function can directly modify the original variable.
C.The address of the actual parameter is passed to the function.
D.A copy of the actual parameter's value is passed to the function.
Correct Answer: A copy of the actual parameter's value is passed to the function.
Explanation:
In Pass by Value, the value of the argument is copied into the formal parameter. Changes made to the parameter inside the function do not affect the original argument.
Incorrect! Try again.
4Which storage class in C/C++ preserves the value of a local variable between function calls?
A.extern
B.register
C.auto
D.static
Correct Answer: static
Explanation:
A static local variable is initialized only once and retains its value between function calls, as it is stored in the data segment rather than the stack.
Incorrect! Try again.
5Which concept in OOP allows a class to derive properties and behavior from another class?
A.Abstraction
B.Inheritance
C.Polymorphism
D.Encapsulation
Correct Answer: Inheritance
Explanation:
Inheritance is the mechanism where a new class (derived class) acquires the properties and methods of an existing class (base class).
Incorrect! Try again.
6In Java, objects are stored in which area of memory?
A.Register
B.ROM
C.Heap
D.Stack
Correct Answer: Heap
Explanation:
In Java, all objects are dynamically allocated on the Heap. The references to these objects may be stored on the Stack.
Incorrect! Try again.
7What is the result of dereferencing a NULL pointer in C++?
A.It causes a segmentation fault or undefined behavior.
B.It creates a new object.
C.It returns a random integer.
D.It returns 0.
Correct Answer: It causes a segmentation fault or undefined behavior.
Explanation:
Accessing memory at address 0 (NULL) is illegal in most operating systems, leading to a crash or undefined behavior.
Incorrect! Try again.
8Which of the following creates a Memory Leak?
A.Passing a pointer by reference.
B.Allocating memory on the stack.
C.Using a global variable.
D.Allocating memory on the heap and failing to free it.
Correct Answer: Allocating memory on the heap and failing to free it.
Explanation:
A memory leak occurs when dynamically allocated memory (Heap) is no longer needed but is not released back to the operating system.
Incorrect! Try again.
9What is Dynamic Binding (Late Binding)?
A.Hardcoding function addresses.
B.Binding variable names to memory addresses during coding.
C.Linking a function call to the code to be executed at runtime.
D.Linking a function call to the code to be executed at compile time.
Correct Answer: Linking a function call to the code to be executed at runtime.
Explanation:
Dynamic binding resolves function calls at runtime, typically used to implement polymorphism (e.g., Virtual Functions in C++).
Incorrect! Try again.
10In C++, what is the this pointer?
A.A global pointer accessible by all classes.
B.A const pointer that points to the object invoking the member function.
C.A pointer to the derived class.
D.A pointer to the base class.
Correct Answer: A const pointer that points to the object invoking the member function.
Explanation:
this is an implicit pointer passed to all non-static member functions, pointing to the object on which the function is called.
Incorrect! Try again.
11Which Java keyword is used to prevent a class from being inherited?
A.abstract
B.const
C.static
D.final
Correct Answer: final
Explanation:
In Java, declaring a class as final prevents it from being subclassed.
Incorrect! Try again.
12In C, if int arr[5] = {1, 2, 3, 4, 5}; and int *p = arr;, what is the value of *(p + 2)?
A.2
B.Address of 3
C.1
D.3
Correct Answer: 3
Explanation:
p points to arr[0]. p + 2 points to arr[2]. Dereferencing *(p + 2) gives the value at index 2, which is 3.
Incorrect! Try again.
13What is a Dangling Pointer?
A.A pointer pointing to a memory location that has been deleted or freed.
B.A pointer that has not been initialized.
C.A pointer pointing to NULL.
D.A pointer to a constant value.
Correct Answer: A pointer pointing to a memory location that has been deleted or freed.
Explanation:
If the memory a pointer points to is deallocated, but the pointer is not set to NULL, it becomes a dangling pointer, pointing to invalid memory.
Incorrect! Try again.
14Which feature of OOP describes wrapping data and methods into a single unit?
A.Inheritance
B.Recursion
C.Polymorphism
D.Encapsulation
Correct Answer: Encapsulation
Explanation:
Encapsulation is the bundling of data (variables) and methods (functions) that operate on that data into a single unit (class).
Incorrect! Try again.
15In Java, parameter passing is always:
A.Pass by Value
B.Pass by Reference
C.Pass by Pointer
D.Pass by Name
Correct Answer: Pass by Value
Explanation:
Java strictly uses Pass by Value. For objects, the 'value' passed is the reference (pointer/address) to the object, not the object itself, but the mechanism is still passing that reference by value.
Incorrect! Try again.
16What is the purpose of a Virtual Destructor in C++?
A.To allow multiple inheritance.
B.To ensure the derived class destructor is called when deleting via a base class pointer.
C.To delete objects faster.
D.To prevent the class from being instantiated.
Correct Answer: To ensure the derived class destructor is called when deleting via a base class pointer.
Explanation:
If a base class destructor is not virtual, deleting a derived object through a base pointer results in undefined behavior (usually only the base destructor runs), causing memory leaks.
Incorrect! Try again.
17Which area of memory is used for recursive function calls and local variables?
A.Heap
B.Code Segment
C.Data Segment
D.Stack
Correct Answer: Stack
Explanation:
The Stack is used for static memory allocation, including function call frames (activation records), local variables, and return addresses.
Incorrect! Try again.
18In C++, what is the size of an empty class?
A.1 byte
B.0 bytes
C.4 bytes
D.2 bytes
Correct Answer: 1 byte
Explanation:
The C++ standard ensures that an empty class has a size of at least 1 byte to ensure that two different objects will have different addresses.
Incorrect! Try again.
19What is the default access specifier for members of a class in C++?
A.protected
B.internal
C.public
D.private
Correct Answer: private
Explanation:
In C++, class members are private by default. In struct, they are public by default.
Incorrect! Try again.
20Which of the following correctly declares a pointer to a function accepting an integer and returning void in C++?
A.void func(int *);
B.*void func(int);
C.void (*func)(int);
D.void *func(int);
Correct Answer: void (*func)(int);
Explanation:
The syntax ReturnType (*PointerName)(ParameterTypes) creates a function pointer. Parentheses around *func are necessary to distinguish it from a function returning a pointer.
Incorrect! Try again.
21What implies the concept of Polymorphism?
A.One interface, multiple methods.
B.Automatic memory management.
C.Hiding data implementation.
D.Creating new classes from old ones.
Correct Answer: One interface, multiple methods.
Explanation:
Polymorphism allows objects to be treated as instances of their parent class, enabling a single interface (method call) to behave differently based on the actual object type.
Incorrect! Try again.
22What is the output of sizeof(char*) on a standard 64-bit architecture?
A.4
B.16
C.8
D.1
Correct Answer: 8
Explanation:
On a 64-bit architecture, memory addresses are 64 bits long, so a pointer (regardless of the data type it points to) occupies 8 bytes.
Incorrect! Try again.
23In Java, what mechanism handles the deallocation of memory on the Heap?
A.free() function
B.Garbage Collector
C.delete operator
D.Destructors
Correct Answer: Garbage Collector
Explanation:
Java uses automatic Garbage Collection to identify and free memory that is no longer reachable by the program, relieving the programmer of manual memory management.
Incorrect! Try again.
24What is a Reference in C++?
A.A variable stored in the CPU register.
B.A pointer that can be null.
C.A duplicate copy of a variable.
D.An alias for an existing variable.
Correct Answer: An alias for an existing variable.
Explanation:
A reference is essentially an alias or an alternative name for an existing variable. It must be initialized upon declaration and cannot be null.
Incorrect! Try again.
25Which problem arises when two pointers point to the same memory location and one is freed?
A.Dangling Pointer
B.Memory Leak
C.Buffer Underrun
D.Stack Overflow
Correct Answer: Dangling Pointer
Explanation:
When one pointer frees the memory, the second pointer still holds the address of that now-invalid memory, becoming a dangling pointer.
Incorrect! Try again.
26What is the Diamond Problem in C++?
A.A memory leak in a loop.
B.Ambiguity arising from multiple inheritance where two base classes inherit from a common ancestor.
C.An issue with pointer arithmetic on 2D arrays.
D.A syntax error in template classes.
Correct Answer: Ambiguity arising from multiple inheritance where two base classes inherit from a common ancestor.
Explanation:
The Diamond Problem occurs when a class inherits from two classes that both inherit from the same super-parent, potentially leading to two copies of the super-parent's members.
Incorrect! Try again.
27What is a Pure Virtual Function in C++?
A.A function that does not use any variables.
B.A virtual function initialized to 0, forcing derived classes to override it.
C.A function with no return type.
D.A function that cannot be overridden.
Correct Answer: A virtual function initialized to 0, forcing derived classes to override it.
Explanation:
Syntax: virtual void func() = 0;. This makes the class Abstract, and the function must be implemented by concrete derived classes.
Incorrect! Try again.
28In the context of parameter passing, what is an Activation Record?
A.A log of compilation errors.
B.A database of class methods.
C.A record of all heap allocations.
D.A data structure stored on the stack containing local variables and return addresses for a function call.
Correct Answer: A data structure stored on the stack containing local variables and return addresses for a function call.
Explanation:
Also known as a stack frame, the activation record holds the state of a specific function invocation.
Incorrect! Try again.
29Which of the following is true about Pass by Reference?
A.It avoids copying large objects, improving performance.
B.It prevents the function from modifying the argument.
C.It passes a copy of the object.
D.It requires more memory than pass by value.
Correct Answer: It avoids copying large objects, improving performance.
Explanation:
Pass by Reference passes the address/alias, avoiding the overhead of copying the entire object data, which is efficient for large structures/classes.
Incorrect! Try again.
30In C++, int &r = x; is an example of:
A.Pointer declaration
B.Reference declaration
C.Bitwise AND
D.Address-of operator
Correct Answer: Reference declaration
Explanation:
The & in a type declaration context (left side of assignment) signifies a reference variable.
Incorrect! Try again.
31What defines a Deep Copy?
A.Copying only the pointer address.
B.Copying the object bit-by-bit.
C.Using the assignment operator =.
D.Copying the object and allocating new memory for the data pointed to by the object.
Correct Answer: Copying the object and allocating new memory for the data pointed to by the object.
Explanation:
A deep copy duplicates the actual data pointed to by pointers within the object, ensuring the new object is fully independent of the original.
Incorrect! Try again.
32Which operator is used to release memory allocated by new[]?
A.delete[]
B.delete
C.remove
D.free
Correct Answer: delete[]
Explanation:
When allocating arrays with new[], you must use delete[] to ensure destructors are called for every element in the array.
Incorrect! Try again.
33Which of the following is NOT a principle of OOP?
A.Compilation
B.Inheritance
C.Encapsulation
D.Polymorphism
Correct Answer: Compilation
Explanation:
Compilation is a process of converting code to machine language, not a conceptual pillar of Object-Oriented Programming.
Incorrect! Try again.
34In Java, String objects are immutable. What does this mean?
A.They cannot be deleted.
B.They cannot be passed to functions.
C.They are stored in the stack only.
D.Their values cannot be changed after creation.
Correct Answer: Their values cannot be changed after creation.
Explanation:
Immutable means once a String object is created, its content cannot be altered. Operations that appear to modify it actually create a new String object.
Incorrect! Try again.
35What is the difference between struct and class in C++ regarding inheritance?
A.There is no difference.
B.Structs cannot inherit.
C.Structs inherit publicly by default; classes inherit privately by default.
D.Classes cannot inherit from structs.
Correct Answer: Structs inherit publicly by default; classes inherit privately by default.
Explanation:
The only technical differences are default access specifiers for members and default inheritance type (public for struct, private for class).
Incorrect! Try again.
36If a recursive function does not have a base case, what error occurs?
A.Stack Overflow
B.Compilation Error
C.Heap Overflow
D.Linker Error
Correct Answer: Stack Overflow
Explanation:
Infinite recursion fills up the stack with activation records until no space remains, causing a Stack Overflow.
Incorrect! Try again.
37What is Function Overloading?
A.Passing too many parameters to a function.
B.Defining multiple functions with the same name but different parameters.
C.Calling a function inside itself.
D.Defining a function in the base and derived class with the same signature.
Correct Answer: Defining multiple functions with the same name but different parameters.
Explanation:
Overloading allows multiple functions with the same name to exist if their parameter lists (types or number of arguments) differ.
Incorrect! Try again.
38In C++, the keyword friend allows a function to:
A.Inherit from the class.
B.Access private and protected members of a class.
C.Delete the class object.
D.Become a member of the class.
Correct Answer: Access private and protected members of a class.
Explanation:
A friend function is not a member of the class but is granted permission to access its non-public members.
Incorrect! Try again.
39Which segment of memory stores global and static variables?
A.Code Segment
B.Heap
C.Stack
D.Data Segment
Correct Answer: Data Segment
Explanation:
Global and static variables are stored in the Data Segment (initialized) or BSS (uninitialized) and exist for the lifetime of the program.
Incorrect! Try again.
40What is the outcome of void *p; in C++?
A.It declares a null pointer.
B.It declares a generic pointer that can hold the address of any data type.
C.It is a syntax error.
D.It declares a pointer that returns nothing.
Correct Answer: It declares a generic pointer that can hold the address of any data type.
Explanation:
A void pointer is a generic pointer. It cannot be dereferenced directly without casting because the compiler doesn't know the size of the data it points to.
Incorrect! Try again.
41Which of the following best describes an Abstract Class?
A.A class with only static methods.
B.A class that has no variables.
C.A class that is hidden from other files.
D.A class that cannot be instantiated and is meant to be subclassed.
Correct Answer: A class that cannot be instantiated and is meant to be subclassed.
Explanation:
Abstract classes serve as blueprints. In C++, they contain at least one pure virtual function. In Java, they are declared with the abstract keyword.
Incorrect! Try again.
42In C++, what is the syntax to access a member of a class using a pointer to an object?
A..
B.
C.
D.
Correct Answer:
Explanation:
The arrow operator () is used to access members of a structure or class via a pointer. ptr->member is equivalent to (*ptr).member.
Incorrect! Try again.
43What is RAII (Resource Acquisition Is Initialization) in C++?
A.A way to pass parameters.
B.A programming idiom where resource management is tied to object lifetime.
C.A compiler optimization technique.
D.A method to initialize arrays.
Correct Answer: A programming idiom where resource management is tied to object lifetime.
Explanation:
RAII ensures resources (like memory or file handles) are acquired in a constructor and released in the destructor, preventing leaks even if exceptions occur.
Method overriding relies on dynamic binding (runtime polymorphism) to determine which version of the method (base or derived) to execute based on the object's actual type.
Incorrect! Try again.
45In Java, does System.gc() force Garbage Collection immediately?
A.Yes, but only for the Stack.
B.No, it creates a new memory segment.
C.Yes, always.
D.No, it only suggests to the JVM that garbage collection is required.
Correct Answer: No, it only suggests to the JVM that garbage collection is required.
Explanation:
The JVM decides when to run the Garbage Collector. System.gc() is a request, not a command, and the JVM may ignore it.
Incorrect! Try again.
46What is the result of int *p = (int*)malloc(sizeof(int)); if memory allocation fails?
A.Returns a dangling pointer.
B.Throws an exception.
C.Terminates the program immediately.
D.Returns NULL.
Correct Answer: Returns NULL.
Explanation:
In C, malloc returns NULL if the system cannot allocate the requested memory block.
Incorrect! Try again.
47Which of the following is true about a Constructor?
A.It has the same name as the class and no return type.
B.It can be static.
C.It must have a return type of void.
D.It can be virtual.
Correct Answer: It has the same name as the class and no return type.
Explanation:
Constructors initializes objects. They cannot return values (not even void), cannot be virtual, and cannot be static.
Incorrect! Try again.
48How does passing a large object by value affect memory?
A.It allocates memory on the heap.
B.It saves memory.
C.It consumes more stack memory due to copying.
D.It has no effect on memory.
Correct Answer: It consumes more stack memory due to copying.
Explanation:
Pass by value creates a copy of the argument on the stack. For large objects, this copy operation consumes significant stack space and CPU time.
Incorrect! Try again.
49What is the primary role of the protected access specifier?
A.Accessible everywhere.
B.Accessible within the class and its derived classes.
C.Accessible only by friend functions.
D.Accessible only within the class.
Correct Answer: Accessible within the class and its derived classes.
Explanation:
protected members are similar to private members but are also accessible to classes that inherit from the base class.
Incorrect! Try again.
50Which pointer arithmetic operation is valid?
A.Adding two pointers.
B.Subtracting two pointers of the same type.
C.Multiplying a pointer by a constant.
D.Dividing a pointer by a constant.
Correct Answer: Subtracting two pointers of the same type.
Explanation:
Subtracting two pointers returns the number of elements (offset) between them. Adding, multiplying, or dividing pointers is invalid.