Unit 6 - Practice Quiz

CSE101

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

A. class
B. struct
C. structure
D. object

2 In the following structure declaration, what is s1?

c
struct Student {
int id;
char name[20];
} s1;

A. A structure tag
B. A structure member
C. A structure variable (object)
D. A structure pointer

3 Which operator is used to access a member of a structure using a structure variable?

A. Arrow operator (->)
B. Dot operator (.)
C. Scope resolution operator (::)
D. Dereference operator (*)

4 Given the pointer ptr pointing to a structure variable, which syntax correctly accesses the member age?

A. ptr.age
B. ptr->age
C. *ptr.age
D. ptr::age

5 What is the primary difference between a Structure and a Union regarding memory allocation?

A. Structures allocate memory for only the largest member, while Unions allocate for all members.
B. Unions allocate memory for all members, while Structures allocate for only the first member.
C. Structures allocate separate memory for each member, while Unions share the same memory location for all members.
D. There is no difference in memory allocation.

6 Consider the following union. Assuming int is 4 bytes and char is 1 byte, what is the size of u?

c
union Data {
int i;
char c;
} u;

A. 1 byte
B. 4 bytes
C. 5 bytes
D. 8 bytes

7 In C++, which header file is required to use cin and cout?

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

8 Which operator is used with cout for outputting data?

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

9 Which namespace must be used or included to access standard C++ libraries like cout without the std:: prefix?

A. using standard namespace;
B. using namespace std;
C. include namespace std;
D. define namespace std;

10 What is the default access specifier for members of a class in C++?

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

11 What is the default access specifier for members of a struct in C++?

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

12 How do you access a global variable if there is a local variable with the same name in the current scope?

A. Use the extern keyword
B. Use the scope resolution operator ::
C. Use the global keyword
D. It is impossible to access the global variable

13 Which of the following correctly defines a class named Box?

A. class Box { ... }
B. class Box { ... };
C. struct Box { ... }
D. object Box { ... };

14 What is an inline function?

A. A function that is called inside another function.
B. A function where the compiler replaces the function call with the actual function code.
C. A function that takes no arguments.
D. A function defined inside a structure only.

15 Member functions defined inside the class body are treated as:

A. static functions
B. virtual functions
C. inline functions
D. private functions

16 Which keyword is used to declare a static data member in a class?

A. const
B. extern
C. static
D. volatile

17 Which of the following is true about static data members?

A. A separate copy is created for every object.
B. They can only be accessed by static member functions.
C. Only one copy exists and is shared by all objects of the class.
D. They cannot be initialized.

18 How are static data members typically initialized?

A. Inside the class definition.
B. Inside the constructor.
C. Outside the class definition using the scope resolution operator.
D. They are automatically initialized to random values.

19 What is a characteristic of static member functions?

A. They can access non-static data members.
B. They contain the this pointer.
C. They can only access static data members and other static member functions.
D. They must be virtual.

20 Which programming paradigm focuses on 'functions' and 'procedures' acting on data?

A. Object-Oriented Programming (OOP)
B. Procedural Programming
C. Event-Driven Programming
D. Declarative Programming

21 Which programming paradigm focuses on modeling real-world entities using 'objects'?

A. Procedural Programming
B. Object-Oriented Programming (OOP)
C. Assembly Programming
D. Functional Programming

22 In Object-Oriented Programming, what is the term for wrapping data and functions together into a single unit?

A. Inheritance
B. Polymorphism
C. Encapsulation
D. Abstraction

23 Consider the following nested structure:

c
struct Date { int d, m, y; };
struct Emp {
int id;
struct Date dob;
} e1;

How do you access the month m of employee e1?

A. e1.m
B. e1.Date.m
C. e1.dob.m
D. dob.m

24 What happens if you assign one structure variable to another of the same type (e.g., struct1 = struct2)?

A. It causes a compilation error.
B. It copies the memory address.
C. It performs a member-wise copy of the data.
D. It creates a pointer link between them.

25 What is the correct syntax to define a method display outside the class MyClass?

A. void display() { ... }
B. void MyClass.display() { ... }
C. void MyClass::display() { ... }
D. void MyClass->display() { ... }

26 Which of the following is valid regarding initialized structures?

c
struct Point { int x; int y; };
struct Point p = {10, 20};

A. p.x is 10 and p.y is 20
B. p.x is 20 and p.y is 10
C. p.x is 10 and p.y is undefined
D. This syntax is invalid

27 Procedural programming generally follows which design approach?

A. Bottom-Up Approach
B. Top-Down Approach
C. Inside-Out Approach
D. None of the above

28 What is an Enumeration (enum)?

A. A collection of variables of different types.
B. A user-defined data type consisting of a set of named integer constants.
C. A type of class with only public members.
D. A pointer to a function.

29 In the following enum, what is the value of BLUE?

c
enum Colors { RED, GREEN=5, BLUE };

A. 2
B. 6
C. 1
D.

30 Which operator is used to extract data from the input stream cin?

A. <<
B. >>
C. &
D. ::

31 Which of the following statements about unions is FALSE?

A. A union can contain members of different data types.
B. All members of a union share the same memory address.
C. Changing one member of a union does not affect the other members.
D. The size of the union is the size of its largest member.

32 Can a structure contain a pointer to itself?

A. No, it causes infinite recursion during compilation.
B. Yes, this is called a self-referential structure.
C. Only if the structure is declared static.
D. Only in C++, not in C.

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

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

34 In the context of OOP, an Object is an instance of a __.

A. Function
B. Structure (C-style)
C. Class
D. Union

35 Why is typedef often used with structures in C?

A. To hide the data members.
B. To create an alias so the keyword struct isn't needed for variable declaration.
C. To reduce the memory usage.
D. To automatically initialize members.

36 What is the output of the following C++ snippet?

cpp
int x = 10;
void func() {
int x = 20;
cout << ::x;
}

A. 10
B. 20
C. Garbage value
D. Compilation Error

37 When passing a structure to a function by value:

A. The function receives the address of the structure.
B. Changes made to the structure in the function are reflected in the original structure.
C. A copy of the entire structure is created on the stack.
D. The structure is passed via a register.

38 Which syntax creates an array of 50 Student structures?

A. struct Student list[50];
B. struct list[50] Student;
C. Student list;
D. int list[50];

39 In C++, which keyword is used to bring a specific member of a namespace into the current scope?

A. include
B. using
C. import
D. scope

40 What does the keyword private imply in a class?

A. Members are accessible from anywhere.
B. Members are accessible only within the class and its friends.
C. Members are accessible by derived classes.
D. Members are static.

41 Which of the following is NOT a feature of Object-Oriented Programming?

A. Polymorphism
B. Global data sharing across all functions without restriction
C. Inheritance
D. Encapsulation

42 What is the memory alignment/padding in structures?

A. Removing unused variables.
B. Adding extra bytes to align data with memory boundaries for performance.
C. Compressing the structure size.
D. Allocating memory in the heap.

43 Can functions be defined inside a struct in C (standard C99)?

A. Yes
B. No
C. Yes, but they must be static
D. Yes, but they must be void

44 If a member function is defined outside the class, how can it be made inline?

A. It is automatically inline.
B. By using the inline keyword in the function definition.
C. By using the static keyword.
D. It is not possible.

45 What is the result of the following pointer arithmetic?

c
struct Demo { int a; char b; }; // Assume sizeof(Demo) is 8 bytes due to padding
struct Demo ptr = (struct Demo )1000;
ptr++;

What is the value of ptr?

A. 1001
B. 1004
C. 1008
D. 1012

46 Which syntax allows multiple variables a, b, and c to be output in a single statement?

A. cout << a, b, c;
B. cout << a << b << c;
C. cout >> a >> b >> c;
D. cout.print(a, b, c);

47 What is the scope of a class member declared as public?

A. Accessible only inside the class.
B. Accessible only in derived classes.
C. Accessible anywhere where the object is visible.
D. Accessible only in the file it is declared.

48 In the declaration union Data { int x; float y; };, if we assign x = 10 and then y = 5.5, what happens to x?

A. x remains 10.
B. x is converted to integer 5.
C. x becomes garbage/corrupted because memory is overwritten.
D. x is moved to a new address.

49 Which of the following creates an object obj of class Test?

A. class Test obj;
B. Test obj;
C. obj : Test;
D. new Test obj;

50 Which symbol terminates a C++ statement (like a function call or declaration)?

A. :
B. .
C. ;
D. }