1Which keyword is used to declare a structure in C/C++?
A.structure
B.struct
C.type
D.record
Correct Answer: struct
Explanation:The keyword struct is used to define a structure, which is a user-defined data type representing a collection of related variables.
Incorrect! Try again.
2In a C structure, if no specific alignment or padding is considered, how is the total memory calculated?
A.It is the size of the largest member.
B.It is the sum of the sizes of all members.
C.It is the size of the smallest member.
D.It is dynamically allocated at runtime.
Correct Answer: It is the sum of the sizes of all members.
Explanation:For a structure, memory is allocated for all members. Theoretically, it is the sum of their sizes (though compiler padding often increases this slightly).
Incorrect! Try again.
3Consider the following definition. What is the correct syntax to initialize a structure variable s1?
A.Student s1 = {1, "John"};
B.Student s1 = (1, "John");
C.Student s1; s1 = {1, "John"};
D.Student s1 [1, "John"];
Correct Answer: Student s1 = {1, "John"};
Explanation:Structures are initialized using curly braces {} containing the values for the members in the order they are declared.
Incorrect! Try again.
4Which operator is used to access a structure member using a structure variable (instance)?
A.Arrow operator ()
B.Dot operator ()
C.Scope resolution operator ()
D.Dereference operator ()
Correct Answer: Dot operator ()
Explanation:The dot operator () is used to access members of a structure when working with the object directly (e.g., s1.name).
Incorrect! Try again.
5Which operator is used to access a structure member using a pointer to the structure?
A.Arrow operator ()
B.Dot operator ()
C.Ampersand operator ()
D.Scope resolution operator ()
Correct Answer: Arrow operator ()
Explanation:When accessing members via a pointer to a structure, the arrow operator () is used. ptr->member is equivalent to (*ptr).member.
Incorrect! Try again.
6What is the primary difference between a Structure and a Union regarding memory allocation?
A.Structures allocate memory for all members; Unions allocate memory only for the largest member.
B.Unions allocate memory for all members; Structures allocate memory only for the largest member.
C.Both allocate memory equal to the sum of all members.
D.Structures require dynamic memory allocation, Unions do not.
Correct Answer: Structures allocate memory for all members; Unions allocate memory only for the largest member.
Explanation:In a union, all members share the same memory location. Therefore, the size of the union is determined by the size of its largest member.
Incorrect! Try again.
7Given the following union, what is sizeof(u) assuming int is 4 bytes and double is 8 bytes?
A.4 bytes
B.8 bytes
C.12 bytes
D.16 bytes
Correct Answer: 8 bytes
Explanation:The size of a union is the size of its largest member. Here, double (8 bytes) is larger than int (4 bytes), so the size is 8 bytes.
Incorrect! Try again.
8In a union, if you modify the value of one member, what happens to the other members?
A.They retain their old values.
B.They are automatically set to zero.
C.Their values may become corrupted or change because they share the same memory.
D.A compiler error occurs.
Correct Answer: Their values may become corrupted or change because they share the same memory.
Explanation:Since all members of a union share the same memory address, writing to one member overwrites the data accessed by the others.
Incorrect! Try again.
9How do you access the member city in the following nested structure?
A.emp.addr.city
B.emp->addr.city
C.emp.city.addr
D.addr.city.emp
Correct Answer: emp.addr.city
Explanation:To access a member of a nested structure, you chain the dot operators: first access the inner structure (emp.addr), then its member (.city).
Incorrect! Try again.
10Which header file is standard for Input/Output operations in C++?
A.<stdio.h>
B.<iostream>
C.<conio.h>
D.<stdlib.h>
Correct Answer: <iostream>
Explanation:The <iostream> header file declares the objects cin, cout, cerr, and clog used for standard I/O in C++.
Incorrect! Try again.
11Which object is used for standard output in C++?
A.cin
B.cout
C.printf
D.write
Correct Answer: cout
Explanation:cout (character output) is the instance of the ostream class used to display output to the standard output device (usually the screen).
Incorrect! Try again.
12Which operator is used with cin for reading input?
A.Insertion operator ()
B.Extraction operator ()
C.Dot operator ()
D.Scope resolution operator ()
Correct Answer: Extraction operator ()
Explanation:The extraction operator >> is used with cin to extract data from the input stream and store it in variables.
Incorrect! Try again.
13Which operator is used with cout for printing output?
A.Insertion operator ()
B.Extraction operator ()
C.Address-of operator ()
D.Ternary operator ()
Correct Answer: Insertion operator ()
Explanation:The insertion operator << is used with cout to insert data into the output stream.
Incorrect! Try again.
14What is the key difference between Procedural Programming and Object-Oriented Programming (OOP)?
A.Procedural focuses on data; OOP focuses on functions.
B.Procedural focuses on functions (procedures); OOP focuses on objects combining data and behavior.
C.Procedural programming supports inheritance, while OOP does not.
D.There is no difference.
Correct Answer: Procedural focuses on functions (procedures); OOP focuses on objects combining data and behavior.
Explanation:Procedural programming is structure-oriented (functions acting on data), whereas OOP organizes software design around data, or objects, rather than functions and logic.
Incorrect! Try again.
15What is a 'Class' in C++?
A.A built-in data type like int or float.
B.A blueprint or template for creating objects.
C.A function that returns a value.
D.A standard library header.
Correct Answer: A blueprint or template for creating objects.
Explanation:A class is a user-defined data type that acts as a blueprint/template, defining the data members and member functions that its objects will have.
Incorrect! Try again.
16By default, the members of a C++ Class are:
A.public
B.private
C.protected
D.static
Correct Answer: private
Explanation:In C++, if no access specifier is provided, class members are private by default. In contrast, struct members are public by default.
Incorrect! Try again.
17By default, the members of a C++ Structure (struct) are:
A.public
B.private
C.protected
D.virtual
Correct Answer: public
Explanation:To maintain backward compatibility with C, members of a struct in C++ are public by default.
Incorrect! Try again.
18Which keyword is used to declare an inline function?
A.static
B.inline
C.const
D.virtual
Correct Answer: inline
Explanation:The inline keyword suggests to the compiler to substitute the function body at the place of the function call to reduce function call overhead.
Incorrect! Try again.
19What is the main advantage of an inline function?
A.It reduces the executable size.
B.It reduces function call overhead (stack operations).
C.It allows recursion to work faster.
D.It hides the function definition.
Correct Answer: It reduces function call overhead (stack operations).
Explanation:Inline functions eliminate the overhead of pushing arguments to the stack and jumping to the function address, as the code is expanded at the call site.
Incorrect! Try again.
20When defining a member function outside the class definition, which operator is used to bind the function to the class?
A.Dot operator ()
B.Arrow operator ()
C.Scope Resolution Operator ()
D.Colon ()
Correct Answer: Scope Resolution Operator ()
Explanation:The scope resolution operator () is used to define a member function outside the class (e.g., void ClassName::functionName() { ... }).
Incorrect! Try again.
21What is an 'Object' in C++?
A.A function block.
B.An instance of a class.
C.A standard header file.
D.A syntax error.
Correct Answer: An instance of a class.
Explanation:An object is a specific instance of a class. It occupies memory and has the attributes and behaviors defined by the class.
Incorrect! Try again.
22Which access modifier allows members to be accessible from outside the class?
A.private
B.protected
C.public
D.hidden
Correct Answer: public
Explanation:Members declared as public are accessible from anywhere the program has access to an object of that class.
Incorrect! Try again.
23Which access modifier restricts access to members only within the class itself (and friends)?
A.public
B.private
C.extern
D.global
Correct Answer: private
Explanation:private members cannot be accessed directly from outside the class, ensuring data hiding.
Incorrect! Try again.
24What characteristic defines a Static Data Member in a class?
A.A separate copy is created for every object.
B.It is stored in the stack memory.
C.One copy is shared by all objects of the class.
D.It cannot be initialized.
Correct Answer: One copy is shared by all objects of the class.
Explanation:A static data member is shared among all instances of the class. Only one copy of the static variable exists in memory, regardless of how many objects are created.
Incorrect! Try again.
25How must a non-const static data member be initialized?
A.Inside the class constructor.
B.Inside the class definition.
C.Outside the class definition, using the scope resolution operator.
D.It is automatically initialized to garbage values.
Correct Answer: Outside the class definition, using the scope resolution operator.
Explanation:Static data members must be defined and initialized outside the class (usually in a .cpp file) using the syntax Type ClassName::VariableName = value;.
Incorrect! Try again.
26What can a Static Member Function access?
A.Only other static data members and static member functions.
B.All data members (static and non-static).
C.Only private non-static members.
D.The this pointer.
Correct Answer: Only other static data members and static member functions.
Explanation:Static member functions do not have a this pointer and are not associated with a specific object instance. Therefore, they can only access static members directly.
Incorrect! Try again.
27How do you call a static member function func() of class Test without creating an object?
A.Test.func();
B.Test::func();
C.func(Test);
D.It is impossible.
Correct Answer: Test::func();
Explanation:Static member functions belong to the class rather than an object and can be called using the class name and scope resolution operator: ClassName::functionName().
Incorrect! Try again.
28Which of the following is NOT a feature of Object-Oriented Programming?
A.Encapsulation
B.Polymorphism
C.Inheritance
D.Global variables as the primary data storage
Correct Answer: Global variables as the primary data storage
Explanation:OOP emphasizes encapsulation and data hiding within objects, minimizing the use of global variables which is more common in older procedural or unstructured paradigms.
Incorrect! Try again.
29What is the size of an empty class in C++?
A.0 bytes
B.1 byte
C.2 bytes
D.4 bytes
Correct Answer: 1 byte
Explanation:In C++, an empty class has a size of at least 1 byte (nonzero) to ensure that two different objects of the same class have different addresses.
Incorrect! Try again.
30Which statement best describes std in C++ programs?
A.It is a function.
B.It is a class.
C.It is a namespace.
D.It is a macro.
Correct Answer: It is a namespace.
Explanation:std is the standard namespace that contains C++ standard library classes and functions (like cout, cin, string).
Incorrect! Try again.
31Regarding structure declaration, what is the role of the semicolon () at the end of the curly braces?
A.It is optional.
B.It terminates the structure definition.
C.It starts the main function.
D.It declares a pointer.
Correct Answer: It terminates the structure definition.
Explanation:A structure definition must always end with a semicolon after the closing brace: struct Name { ... };.
Incorrect! Try again.
32Can a structure contain a pointer to itself?
A.No, it causes infinite recursion during compilation.
B.Yes, this is known as a self-referential structure.
C.Only if the structure is static.
D.Only in C++, not in C.
Correct Answer: Yes, this is known as a self-referential structure.
Explanation:A structure can contain a pointer to an instance of the same structure type. This is essential for creating data structures like linked lists and trees.
Incorrect! Try again.
33What is the correct way to read a string with spaces (e.g., "Hello World") into a character array str using cin?
A.cin >> str;
B.cin.getline(str, size);
C.cin.read(str);
D.cout << str;
Correct Answer: cin.getline(str, size);
Explanation:The standard extraction operator >> stops reading at the first whitespace. cin.getline() reads a whole line including spaces.
Incorrect! Try again.
34If you define a function inside the class definition, it is treated as:
A.A static function.
B.An inline function (implicitly).
C.A virtual function.
D.A friend function.
Correct Answer: An inline function (implicitly).
Explanation:Member functions defined entirely inside the class body are implicitly treated as inline functions by the compiler.
Incorrect! Try again.
35What is the output of cout << sizeof(char) << sizeof(int); on a typical 32-bit system?
A.12
B.14
C.24
D.88
Correct Answer: 14
Explanation:sizeof(char) is 1 and sizeof(int) is usually 4. The output stream concatenates these numbers, resulting in "14" (1 followed by 4, assuming no spaces were inserted).
Incorrect! Try again.
36Which of the following creates an array of 10 objects of class Robot?
A.Robot obj;
B.Robot obj[10];
C.obj Robot[10];
D.class Robot[10];
Correct Answer: Robot obj[10];
Explanation:The syntax ClassName arrayName[Size]; creates an array of objects. Robot obj[10]; creates 10 instances of Robot.
Incorrect! Try again.
37Can a Union contain a member which is a Structure?
A.Yes
B.No
C.Only if the structure is empty
D.Only in C, not C++
Correct Answer: Yes
Explanation:Structures and unions can be nested within each other arbitrarily.
Incorrect! Try again.
38What is the primary usage of using namespace std;?
A.To include the iostream file.
B.To allow access to names in the std namespace without the std:: prefix.
C.To define a new namespace called std.
D.To increase compilation speed.
Correct Answer: To allow access to names in the std namespace without the std:: prefix.
Explanation:It brings all identifiers from the std namespace into the current scope, so you can write cout instead of std::cout.
Incorrect! Try again.
39Which of the following is true regarding Enumerations (enum) vs Macros (#define) for constants?
A.Enums are handled by the preprocessor; Macros are handled by the compiler.
B.Enums follow scope rules; Macros are global (unless undefined).
C.Macros have a specific type; Enums do not.
D.There is no difference.
Correct Answer: Enums follow scope rules; Macros are global (unless undefined).
Explanation:Enums respect C++ scoping rules (they can be local to a class or function), whereas preprocessor macros (#define) apply globally from the point of definition.
Incorrect! Try again.
40Which operator cannot be used to access members of a class directly?
A..
B.->
C.::
D.#
Correct Answer: #
Explanation:# is a preprocessor directive symbol, not an operator for accessing class members.
Incorrect! Try again.
41In a C++ class, if a member function does not modify any data members, it should be declared as:
A.static
B.const
C.mutable
D.inline
Correct Answer: const
Explanation:A const member function (e.g., void func() const;) guarantees that it will not modify the object's data members.
Incorrect! Try again.
42What does the this pointer represent in a non-static member function?
A.Pointer to the class.
B.Pointer to the object invoking the function.
C.Pointer to the main function.
D.Pointer to the previous object.
Correct Answer: Pointer to the object invoking the function.
Explanation:this is a hidden pointer passed to all non-static member functions that points to the object on which the member function is called.
Incorrect! Try again.
43Can we define a structure inside a function?
A.No, structures must be global.
B.Yes, but it can only be used within that function.
C.Yes, and it can be used globally.
D.No, it causes a linker error.
Correct Answer: Yes, but it can only be used within that function.
Explanation:A structure defined inside a function has local scope and is only known/usable within that function.
Incorrect! Try again.
44What is the return type of main() in standard C++?
A.void
B.int
C.float
D.Any type
Correct Answer: int
Explanation:According to the C++ standard, the return type of main must be int. It returns a status code to the operating system.
Incorrect! Try again.
45Which paradigm is best suited for complex systems where entities and their interactions need to be modeled?
A.Procedural Programming
B.Object-Oriented Programming
C.Assembly Language
D.Machine Code
Correct Answer: Object-Oriented Programming
Explanation:OOP is designed to model real-world entities (objects) and their interactions, making it suitable for complex software systems.
Incorrect! Try again.
46What happens if a static member variable is not defined explicitly outside the class?
A.It works fine.
B.The compiler initializes it to zero.
C.A linker error occurs.
D.It becomes a regular variable.
Correct Answer: A linker error occurs.
Explanation:Declaring a static member inside a class is just a declaration. If it is not defined (memory allocated) outside the class, the linker will fail to find the symbol.
Incorrect! Try again.
47Consider struct { int a; } s;. What type of structure is this?
A.Named structure
B.Anonymous structure
C.Inline structure
D.Virtual structure
Correct Answer: Anonymous structure
Explanation:A structure declared without a tag name is called an anonymous structure.
Incorrect! Try again.
48Which of the following is true about cin and data type safety?
A.cin does not check data types.
B.cin automatically handles data types based on the variable provided.
C.cin treats everything as strings.
D.You must specify format specifiers like %d with cin.
Correct Answer: cin automatically handles data types based on the variable provided.
Explanation:Unlike scanf, cin (using operator overloading) detects the type of the variable and formats the input accordingly without needing format specifiers.
Incorrect! Try again.
49Why might a programmer choose a union over a struct?
A.To allow easier debugging.
B.To save memory when only one member is needed at a time.
C.To increase execution speed.
D.To use inheritance.
Correct Answer: To save memory when only one member is needed at a time.
Explanation:The primary use case for a union is memory conservation when a variable can hold one of several data types, but never more than one at a time.
Incorrect! Try again.
50In the context of OOP, what is 'Data Hiding'?
A.Encrypting data on the hard drive.
B.Deleting unused variables.
C.Restricting access to data members using private/protected access specifiers.
D.Storing data in a hidden folder.
Correct Answer: Restricting access to data members using private/protected access specifiers.
Explanation:Data hiding prevents direct access to internal object details from outside code, forcing interaction through defined public interfaces (methods).