Unit 6 - Practice Quiz

CSE101 50 Questions
0 Correct 0 Wrong 50 Left
0/50

1 Which keyword is used to declare a structure in C/C++?

A. structure
B. struct
C. type
D. record

2 In 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.

3 Consider 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"];

4 Which 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 ()

5 Which 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 ()

6 What 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.

7 Given 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

8 In 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.

9 How 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

10 Which header file is standard for Input/Output operations in C++?

A. <stdio.h>
B. <iostream>
C. <conio.h>
D. <stdlib.h>

11 Which object is used for standard output in C++?

A. cin
B. cout
C. printf
D. write

12 Which operator is used with cin for reading input?

A. Insertion operator ()
B. Extraction operator ()
C. Dot operator ()
D. Scope resolution operator ()

13 Which operator is used with cout for printing output?

A. Insertion operator ()
B. Extraction operator ()
C. Address-of operator ()
D. Ternary operator ()

14 What 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.

15 What 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.

16 By default, the members of a C++ Class are:

A. public
B. private
C. protected
D. static

17 By default, the members of a C++ Structure (struct) are:

A. public
B. private
C. protected
D. virtual

18 Which keyword is used to declare an inline function?

A. static
B. inline
C. const
D. virtual

19 What 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.

20 When 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 ()

21 What is an 'Object' in C++?

A. A function block.
B. An instance of a class.
C. A standard header file.
D. A syntax error.

22 Which access modifier allows members to be accessible from outside the class?

A. private
B. protected
C. public
D. hidden

23 Which access modifier restricts access to members only within the class itself (and friends)?

A. public
B. private
C. extern
D. global

24 What 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.

25 How 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.

26 What 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.

27 How 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.

28 Which 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

29 What is the size of an empty class in C++?

A. 0 bytes
B. 1 byte
C. 2 bytes
D. 4 bytes

30 Which 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.

31 Regarding 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.

32 Can 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.

33 What 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;

34 If 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.

35 What is the output of cout << sizeof(char) << sizeof(int); on a typical 32-bit system?

A. 12
B. 14
C. 24
D. 88

36 Which 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];

37 Can 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++

38 What 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.

39 Which 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.

40 Which operator cannot be used to access members of a class directly?

A. .
B. ->
C. ::
D. #

41 In 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

42 What 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.

43 Can 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.

44 What is the return type of main() in standard C++?

A. void
B. int
C. float
D. Any type

45 Which 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

46 What 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.

47 Consider struct { int a; } s;. What type of structure is this?

A. Named structure
B. Anonymous structure
C. Inline structure
D. Virtual structure

48 Which 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.

49 Why 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.

50 In 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.