Unit 1: Principles of OOP and C++ Essentials - Practice Quiz

CAP455 — Object Oriented Programming Using C++ 60 Questions
0 Correct 0 Wrong 60 Left
0/60

1 What is the primary focus of object-oriented programming?

Procedural vs object oriented programming paradigms Easy
A. Functions and procedures
B. Files and directories
C. Labels and jumps
D. Objects and classes

2 Which programming paradigm primarily organizes a program as a sequence of functions?

Procedural vs object oriented programming paradigms Easy
A. Logic programming
B. Procedural programming
C. Object-oriented programming
D. Event-driven programming

3 Which C++ stream object is commonly used to display output on the console?

Input/output streams Easy
A. cout
B. cin
C. cerr
D. clog

4 Which operator is used with cin to read input in C++?

Input/output streams Easy
A. >>
B. &&
C. <<
D. ==

5 What is a class in C++?

Classes and objects Easy
A. A blueprint for objects
B. A header directive
C. A stream operator
D. A built-in loop

6 What is an object in C++?

Classes and objects Easy
A. An instance of a class
B. A compiler command
C. A standard header
D. A type of operator

7 How are the data members of a union stored?

Structure vs union Easy
A. They stay in different files
B. They remain in registers
C. They share the same memory
D. They use separate memory

8 Which statement correctly describes the members of a structure?

Structure vs union Easy
A. All members share one location
B. Members cannot have different types
C. Members are always private
D. Each member has separate storage

9 What does an enumeration define in C++?

Enumerations and classes Easy
A. A group of input streams
B. A collection of functions
C. A set of named constants
D. A sequence of objects

10 Which keyword declares a scoped enumeration in modern C++?

Enumerations and classes Easy
A. static enum
B. enum class
C. union enum
D. public class

11 How many copies of a static data member are shared by all objects of a class?

Static data members and functions Easy
A. One copy
B. One per function
C. Two copies
D. One per object

12 Which type of class function can be called using the class name without creating an object?

Static data members and functions Easy
A. Virtual member function
B. Friend member function
C. Const member function
D. Static member function

13 What is a user-defined function?

User defined functions Easy
A. A function written by the programmer
B. A function supplied by the operating system
C. A function created by the linker
D. A function built into the CPU

14 Which keyword requests that a function be treated as an inline function?

Inline function Easy
A. extern
B. virtual
C. typedef
D. inline

15 Which keyword declares a non-member function that can access a class's private members?

Friend function and friend class Easy
A. protected
B. friend
C. operator
D. private

16 What privilege does a friend class receive?

Friend function and friend class Easy
A. Automatic object creation
B. Access to every program file
C. Access to private members
D. Automatic memory allocation

17 Which symbol is used to declare a reference variable in C++?

Reference variables Easy
A. #
B. %
C. &
D. *

18 What is passed to a function in call by value?

Differentiate among call by value, call by address and call by reference Easy
A. The argument's memory address
B. A copy of the argument
C. The argument's data type
D. A reference to the argument

19 Which kind of parameter is normally used in call by address?

Differentiate among call by value, call by address and call by reference Easy
A. A reference parameter
B. A pointer parameter
C. A value parameter
D. A static parameter

20 What is recursion?

Recursion Easy
A. A loop declaring an object
B. A class creating a stream
C. A union sharing memory
D. A function calling itself

21 A banking application must represent accounts with their balances and restrict balance updates to validated deposit and withdrawal operations. Which design best follows the object-oriented paradigm?

Procedural vs object oriented programming paradigms Medium
A. Encapsulate each balance and its operations inside an Account class
B. Place each banking operation in a separate source file
C. Store balances globally and update them through unrelated functions
D. Store all balances in one array and modify them directly

22 What does the following code print?

int x = 7;

cout << setfill('0') << setw(4) << x;

Input/output streams Medium
A. 7777
B. 7
C. 0007
D. 7000

23 After cin >> age;, a program must read a full name using getline(cin, name);. However, name becomes empty. Which statement should normally be placed before getline?

Input/output streams Medium
A. cin.ignore();
B. cout.ignore();
C. cin.clear();
D. cin.flush();

24 Consider the following class:

class Box { int width; public: void setWidth(int w) { width = w; } };

Which statement about width is correct?

Classes and objects Medium
A. It is static because no access label precedes it
B. It can be accessed directly by every function
C. It is private and accessed by setWidth
D. It can be accessed directly only through an object

25 What is printed by this program fragment?

class Item { public: Item() { cout << "C"; } };

Item items[3];

Classes and objects Medium
A. CCC
B. C
C. CC
D. Nothing

26 A program must store an employee's integer ID, floating-point salary, and character grade simultaneously. Which declaration is most appropriate?

Structure vs union Medium
A. An enumeration containing all three members
B. A structure containing all three members
C. A union containing all three members
D. A reference bound to all three values

27 Given enum class Status { Idle, Running, Stopped };, which statement correctly creates a variable initialized to Running?

Enumerations and classes Medium
A. Status::Running s;
B. Status s = Running;
C. int s = Status::Running;
D. Status s = Status::Running;

28 What is printed by the following code?

class Counter { public: static int count; Counter() { ++count; } };

int Counter::count = 0;

Counter a[3]; Counter b; cout << Counter::count;

Static data members and functions Medium
A. 3
B. 0
C. 1
D. 4

29 A class contains a non-static member value and a static member count. Which operation is valid inside a static member function without receiving an object?

Static data members and functions Medium
A. Access value directly
B. Call every non-static function directly
C. Access this->value directly
D. Access count directly

30 Given int power(int base, int exponent = 2);, which call uses the default argument?

User defined functions Medium
A. power(exponent)
B. power()
C. power(5)
D. power(5, 3)

31 Which pair of function declarations forms a valid overload based on parameter types?

User defined functions Medium
A. int compute(int); and static int compute(int);
B. int compute(int); and double compute(int);
C. int compute(int); and int compute(int);
D. int compute(int); and int compute(double);

32 Consider inline int square(int x) { return x * x; }. If int i = 3; cout << square(i++) << " " << i; is executed, what is printed?

Inline function Medium
A. 9 4
B. 9 5
C. 16 4
D. 12 4

33 A non-member function must compare private values stored in two objects of class Box. Which declaration inside Box grants the required access?

Friend function and friend class Medium
A. virtual bool compare(Box, Box);
B. friend bool compare(Box, Box);
C. static bool compare(Box, Box);
D. inline class compare(Box, Box);

34 If class A declares class B as a friend, which statement is correct?

Friend function and friend class Medium
A. A automatically becomes a friend of B
B. Every object used by B becomes a friend
C. B can access private members of A
D. Subclasses of B become friends of A

35 What is printed by this code?

int x = 4, y = 9;

int& larger = (x > y) ? x : y;

larger = 0;

cout << x << " " << y;

Reference variables Medium
A. 4 0
B. 4 9
C. 0 0
D. 0 9

36 Which declaration correctly creates a reference variable that aliases score?

Reference variables Medium
A. int score = 10; int& ref = 10;
B. int score = 10; int ref& = score;
C. int score = 10; int& ref = score;
D. int score = 10; int& ref;

37 What is printed after this function call?

void update(int v, int* p, int& r) { ++v; ++(*p); ++r; }

int a = 1, b = 1, c = 1; update(a, &b, c); cout << a << b << c;

Differentiate among call by value, call by address and call by reference Medium
A. 111
B. 122
C. 222
D. 212

38 Which function signature allows two integers to be swapped without pointer syntax at the call site?

Differentiate among call by value, call by address and call by reference Medium
A. void swap(const int a, const int b);
B. void swap(int& a, int& b);
C. void swap(int* a, int* b);
D. void swap(int a, int b);

39 What value is returned by sumOdd(5)?

int sumOdd(int n) { if (n <= 1) return 1; return n + sumOdd(n - 2); }

Recursion Medium
A. 8
B. 15
C. 6
D. 9

40 What does the following call print?

void show(int n) { if (n == 0) return; cout << n; show(n - 1); cout << n; }

show(3);

Recursion Medium
A. 123321
B. 321123
C. 123123
D. 321321

41 A program represents bank accounts as records and provides separate functions such as deposit(Account&, double) and withdraw(Account&, double). Which redesign most strongly demonstrates object-oriented encapsulation rather than merely reorganizing the procedural code?

Procedural vs object oriented programming paradigms Hard
A. Replace functions with preprocessor macros
B. Pass every account as a pointer
C. Place all records in a global array
D. Define Account methods that enforce balance invariants

42 Given int x; while (std::cin >> x) { /* process x */ }, what is the most accurate reason the loop terminates?

Input/output streams Hard
A. The stream automatically discards invalid input
B. The stream reaches a newline
C. The extraction operation sets a failure state
D. The extraction operation returns the last integer

43 What is the key language-level distinction between these declarations: class A { int x; }; and struct B { int x; };?

Classes and objects Hard
A. A objects cannot be dynamically allocated
B. B objects cannot contain member functions
C. A supports inheritance but B does not
D. x is private in A and public in B

44 Consider union U { int i; float f; }; U u; u.i = 10; u.f = 2.5f;. Which statement is correct?

Structure vs union Hard
A. The union allocates space for both members
B. Both values remain independently stored
C. Only the most recently written member is active
D. Reading u.i always restores its former value

45 Which initialization is rejected when enum class State { Idle, Busy }; is declared?

Enumerations and classes Hard
A. State s = Idle;
B. State s = State::Idle;
C. State s = State::Busy;
D. State s{State::Busy};

46 For class Counter { static int n; };, which definition is required in a traditional pre-C++17 translation unit if n is odr-used?

Static data members and functions Hard
A. int n::Counter = 0;
B. static int Counter::n = 0;
C. Counter::static int n = 0;
D. int Counter::n = 0;

47 Inside a static member function of class Logger, which access is invalid unless an object is explicitly supplied?

Static data members and functions Hard
A. Accessing a static data member
B. Using a local automatic variable
C. Reading a non-static data member directly
D. Calling another static member function

48 Given void f(int); void f(double = 1.0);, what happens for the call f()?

User defined functions Hard
A. The double overload is selected
B. The call is ambiguous
C. The int overload is selected
D. The program fails only at link time

49 Which statement best describes the inline specifier in modern C++?

Inline function Hard
A. It prevents the function from being recursive
B. It removes all function-call overhead
C. It requires machine-code expansion
D. It permits repeated identical definitions

50 If class Box { friend void inspect(const Box&); private: int value; }; is declared, which statement is correct?

Friend function and friend class Hard
A. Every function in its file gains access
B. Only Box objects can call inspect
C. inspect may access value
D. inspect becomes a member function

51 Class A declares friend class B;. Which conclusion is valid?

Friend function and friend class Hard
A. All classes derived from B access A
B. B may access A's private members
C. B's friends automatically access A
D. A automatically becomes a friend of B

52 After int a = 3, b = 4; int& r = a; r = b;, what are the final values?

Reference variables Hard
A. a is 4 and b is 4
B. a is 3 and b is 4
C. a is 3 and b is 3
D. r is rebound from a to b

53 Which declaration is valid and safely extends the lifetime of the temporary bound to it?

Reference variables Hard
A. int& const r = 5;
B. int& r = 5;
C. int&& r = 5; only as a named reference
D. const int& r = 5;

54 For void f(int x, int* p, int& r) { x++; (*p)++; r++; }, called as int a=1,b=1; f(a,&a,b);, what are a and b afterward?

Differentiate among call by value, call by address and call by reference Hard
A. a = 2, b = 3
B. a = 1, b = 1
C. a = 3, b = 2
D. a = 2, b = 2

55 Which call can be valid for void change(int* p) while still allowing the function to represent "no object supplied"?

Differentiate among call by value, call by address and call by reference Hard
A. change(NULL); always selects an integer overload
B. change(nullptr);
C. change(&nullptr);
D. change(0);

56 What is the principal defect in int sum(int n) { return n + sum(n - 1); } when called with a positive integer?

Recursion Hard
A. The parameter must be passed by reference
B. Recursion cannot return an integer
C. The addition is evaluated before recursion
D. The function has no terminating base case

57 What does this function return for f(4)? int f(int n) { if (n <= 1) return 1; return f(n - 1) + f(n - 2); }

Recursion Hard
A. 8
B. 3
C. 16
D. 5

58 After std::cin >> n; std::getline(std::cin, line);, why can line initially be empty when valid input is entered on one line?

Input/output streams Hard
A. The newline remains in the input buffer
B. Integers cannot precede strings
C. getline ignores all whitespace
D. Extraction disables getline

59 For enum Color { Red = 1, Green, Blue = 1 };, which statement is correct?

Enumerations and classes Hard
A. Enumerators must have unique values
B. Blue cannot equal Red
C. Green has value 1
D. Green has value 2

60 A class has a non-static member int value; but no user-declared constructor. What happens for Record r; when Record is an aggregate and default initialization is used?

Classes and objects Hard
A. value is guaranteed to be zero
B. The declaration is always ill-formed
C. value is value-initialized automatically
D. value has an indeterminate value