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. Labels and jumps
B. Files and directories
C. Functions and procedures
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. Object-oriented programming
B. Procedural programming
C. Event-driven programming
D. Logic programming

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

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

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 built-in loop
B. A blueprint for objects
C. A header directive
D. A stream operator

6 What is an object in C++?

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

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. Members cannot have different types
B. All members share one location
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 set of named constants
C. A sequence of objects
D. A collection of functions

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

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

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 per function
B. One copy
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. Const member function
B. Static member function
C. Friend member function
D. Virtual member function

13 What is a user-defined function?

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

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

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

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

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

16 What privilege does a friend class receive?

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

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. A copy of the argument
B. A reference to the argument
C. The argument's memory address
D. The argument's data type

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 union sharing memory
C. A function calling itself
D. A class creating a stream

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. Store all balances in one array and modify them directly
B. Place each banking operation in a separate source file
C. Encapsulate each balance and its operations inside an Account class
D. Store balances globally and update them through unrelated functions

22 What does the following code print?

int x = 7;

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

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

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. cout.ignore();
B. cin.flush();
C. cin.clear();
D. cin.ignore();

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 can be accessed directly only through an object
B. It is private and accessed by setWidth
C. It can be accessed directly by every function
D. It is static because no access label precedes it

25 What is printed by this program fragment?

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

Item items[3];

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

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. A structure containing all three members
B. A reference bound to all three values
C. A union containing all three members
D. An enumeration containing all three members

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

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

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. 4
B. 0
C. 1
D. 3

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 count directly
B. Access value directly
C. Access this->value directly
D. Call every non-static function directly

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

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

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(double);
D. int compute(int); and int compute(int);

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 5
B. 9 4
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. friend bool compare(Box, Box);
B. virtual 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. Subclasses of B become friends of A
C. B can access private members of A
D. Every object used by B becomes a friend

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. 222
B. 111
C. 122
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(int& a, int& b);
B. void swap(int* a, int* b);
C. void swap(const int a, const 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. 6
B. 9
C. 15
D. 8

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. 123123
C. 321321
D. 321123

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. Pass every account as a pointer
B. Define Account methods that enforce balance invariants
C. Place all records in a global array
D. Replace functions with preprocessor macros

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 extraction operation sets a failure state
B. The extraction operation returns the last integer
C. The stream reaches a newline
D. The stream automatically discards invalid input

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. x is private in A and public in B
B. A supports inheritance but B does not
C. A objects cannot be dynamically allocated
D. B objects cannot contain member functions

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. Both values remain independently stored
B. The union allocates space for both members
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::Busy;
C. State s{State::Busy};
D. State s = State::Idle;

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 Counter::n = 0;
B. Counter::static int n = 0;
C. int n::Counter = 0;
D. static 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. Reading a non-static data member directly
B. Using a local automatic variable
C. Accessing a static data member
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 program fails only at link time
B. The double overload is selected
C. The call is ambiguous
D. The int overload is selected

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

Inline function Hard
A. It removes all function-call overhead
B. It prevents the function from being recursive
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. inspect becomes a member function
B. Only Box objects can call inspect
C. Every function in its file gains access
D. inspect may access value

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. A automatically becomes a friend of B
C. B may access A's private members
D. B's friends automatically access A

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

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

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

Reference variables Hard
A. int&& r = 5; only as a named reference
B. const int& r = 5;
C. int& r = 5;
D. int& const 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 = 3, b = 2
B. a = 1, b = 1
C. a = 2, b = 2
D. a = 2, b = 3

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(0);
D. change(nullptr);

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. Recursion cannot return an integer
B. The parameter must be passed by reference
C. The function has no terminating base case
D. The addition is evaluated before recursion

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. 3
B. 5
C. 16
D. 8

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. Extraction disables getline
B. The newline remains in the input buffer
C. Integers cannot precede strings
D. getline ignores all whitespace

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

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

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 value-initialized automatically
B. value is guaranteed to be zero
C. The declaration is always ill-formed
D. value has an indeterminate value