Unit 1: C++ Programming Basics and Functions - Practice Quiz

CSE202 — Object Oriented Programming 60 Questions
0 Correct 0 Wrong 60 Left
0/60

1 Which OOP concept combines data and the functions that operate on that data into a single unit?

Introduction to concepts of OOP and OOP languages Easy
A. Encapsulation
B. Tokenization
C. Iteration
D. Compilation

2 Which of the following is commonly used as an object-oriented programming language?

Introduction to concepts of OOP and OOP languages Easy
A. C++
B. Assembly
C. HTML
D. SQL

3 Which C++ statement reads a value into the variable age?

Reading and writing data using cin and cout Easy
A. cin >> age;
B. cout >> age;
C. cin << age;
D. cout << age;

4 Which C++ statement displays the word Hello?

Reading and writing data using cin and cout Easy
A. cout << "Hello";
B. cin >> "Hello";
C. cin << "Hello";
D. cout >> "Hello";

5 Which keyword is used to define a class in C++?

Creating classes Easy
A. class
B. define
C. module
D. object

6 Given class Student {};, which statement creates an object named s?

Class objects Easy
A. class s;
B. Student();
C. object Student;
D. Student s;

7 If age is a public member of object s, how is it accessed?

Accessing class members Easy
A. s::age
B. s.age
C. s:age
D. s->age

8 Which statement correctly describes a C++ union?

Differences between structures, unions, enumerations and classes Easy
A. It cannot contain variables
B. Its members are always private
C. Its members share storage
D. It stores only named constants

9 What is the main purpose of an enumeration in C++?

Differences between structures, unions, enumerations and classes Easy
A. To define named constants
B. To allocate shared storage
C. To overload stream operators
D. To create recursive functions

10 A member function defined inside its class definition is generally treated as what kind of function?

Inline and non-inline member functions Easy
A. A friend function
B. A virtual function
C. A recursive function
D. An inline function

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

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

12 What is the primary organizational unit in object-oriented programming?

Differences between procedural and object-oriented programming paradigms Easy
A. Objects
B. Files
C. Instructions
D. Labels

13 Which operator is known as the stream insertion operator in C++?

Features of input/output streams Easy
A. <<
B. ==
C. &&
D. >>

14 What happens when an argument with a default value is omitted in a function call?

Functions with default parameters or arguments Easy
A. The default value is used
B. The program always stops
C. The function is deleted
D. The parameter becomes private

15 Which keyword requests that a function be expanded at the place where it is called?

Inline functions Easy
A. extern
B. inline
C. static
D. friend

16 What does the endl manipulator normally do?

Manipulator functions Easy
A. Changes a variable's data type
B. Ends the entire C++ program
C. Reads a complete input line
D. Adds a newline and flushes output

17 What is function overloading in C++?

Function overloading and scope rules Easy
A. Hiding every function inside a class
B. Using one name with different parameter lists
C. Calling one function from another function
D. Defining a function without parameters

18 Which keyword allows a non-member function to access a class's private members?

Friend function and friend class Easy
A. friend
B. inline
C. public
D. virtual

19 What is a reference variable in C++?

Reference variables Easy
A. An alias for another variable
B. A constant stored in an enum
C. A copy of an entire class
D. A function without a name

20 In call by value, what does a function receive?

Differences between call by value, call by address and call by reference Easy
A. The argument's class definition
B. The argument's memory address
C. A copy of the argument
D. A reference to the argument

21 Consider the following C++ code:

class Shape { public: virtual void draw() { cout << "Shape"; } };

class Circle : public Shape { public: void draw() override { cout << "Circle"; } };

Shape* p = new Circle; p->draw();

Which OOP concept causes Circle to be printed?

Introduction to concepts of OOP and OOP languages Medium
A. Compile-time encapsulation
B. Multiple inheritance
C. Runtime polymorphism
D. Object composition, which copies the complete Circle object into the Shape pointer

22 The following code reads an integer correctly, but name becomes an empty string when the user enters 21 followed by Asha on the next line:

cin >> age;

getline(cin, name);

Which statement should normally be placed between these two statements?

Reading and writing data using cin and cout Medium
A. cout.flush();
B. cin.clear();
C. cin.sync();
D. cin.ignore();

23 What is printed by the following program fragment?

class Point {

int x;

public:

Point(int value) : x(value) {}

int getX() const { return x; }

};

Point p(6);

cout << p.getX();

Creating classes Medium
A. 0
B. A compilation error because every class must provide an explicit default constructor
C. 6
D. A garbage value

24 Consider the following code:

class Box { public: int value; };

Box a; a.value = 5;

Box b = a;

b.value = 9;

cout << a.value << " " << b.value;

What is the output?

Class objects Medium
A. 9 9
B. 5 9
C. 9 5
D. 5 5

25 Given the following declarations, which statement is valid?

class Base { protected: int x = 4; };

class Derived : public Base { public: int read() { return x; } };

Derived d;

Accessing class members Medium
A. cout << Base::x;
B. cout << d.read();
C. cout << d.x;
D. d.Base::x = 8;

26 Which statement correctly compares C++ structures, unions, enumerations, and classes?

Differences between structures, unions, enumerations and classes Medium
A. An enumeration stores all its named constants simultaneously as independently modifiable variables.
B. Every union member has separate storage, while all class members share one storage location.
C. Structure members are public by default, while class members are private by default.
D. Structures cannot contain constructors or member functions, although classes can contain both features.

27 Given class Calc { public: int square(int); };, which definition is a non-inline member-function definition?

Inline and non-inline member functions Medium
A. int inline Calc::square(int x) { return x * x; }
B. int Calc::square(int x) { return x * x; }
C. class Calc { public: int square(int x) { return x * x; } };
D. inline int Calc::square(int x) { return x * x; }

28 What is printed by this code?

class Widget {

public:

static int count;

Widget() { ++count; }

};

int Widget::count = 0;

Widget a, b;

Widget c = a;

cout << Widget::count;

Static data members and static member functions Medium
A. 3
B. 2
C. A compilation error because static data members cannot be modified by constructors
D. 1

29 A banking program must prevent arbitrary code from changing an account balance and must require all withdrawals to be validated. Which design best follows the object-oriented paradigm?

Differences between procedural and object-oriented programming paradigms Medium
A. Store the balance globally and let each procedure modify it directly.
B. Place all account balances in one union so that every operation shares the same memory.
C. Pass the balance by value to every withdrawal procedure.
D. Store the balance privately and modify it through public member functions.

30 Consider the following code:

istringstream input("17abc");

int number;

char letter;

input >> number >> letter;

What values are stored if both extractions succeed?

Features of input/output streams Medium
A. number is 0 and letter is 'a'
B. The integer extraction fails because the complete input token is not a valid integer
C. number is 17 and letter is 'c'
D. number is 17 and letter is 'a'

31 Which function declaration uses default arguments legally in C++?

Functions with default parameters or arguments Medium
A. void process(int a = 1, int b, int c);
B. void process(int a, int b = 2, int c);
C. void process(int a, int b = 2, int c = 3);
D. void process(int a = 1, int b, int c = 3);

32 Which statement about an inline function in C++ is correct?

Inline functions Medium
A. The compiler may choose not to expand the function at the call site.
B. An inline function is automatically executed before main() because its body is processed during compilation.
C. The compiler must replace every function call with the complete function body.
D. An inline function cannot contain loops, local variables, or conditional statements.

33 Assume the required formatting header is included. What does the following statement print?

cout << setfill('0') << setw(4) << 7 << " " << setw(2) << 5;

Manipulator functions Medium
A. 0007 5
B. 7000 50
C. 0007 05
D. 0007 005

34 What is selected by d.show(3) in the following code?

class Base { public: void show(int) { cout << "Base"; } };

class Derived : public Base { public: void show(double) { cout << "Derived"; } };

Derived d;

d.show(3);

Function overloading and scope rules Medium
A. Neither function, because an integer cannot be implicitly converted to a double in an overloaded member call
B. Both functions, in declaration order
C. Base::show(int)
D. Derived::show(double)

35 Why can reveal() access v.code in this code?

class Vault {

int code = 42;

friend int reveal(const Vault&);

};

int reveal(const Vault& v) { return v.code; }

Friend function and friend class Medium
A. reveal() is explicitly declared as a friend of Vault.
B. reveal() becomes an inherited member function of Vault.
C. All non-member functions may read private members of constant objects.
D. Passing Vault by reference automatically changes every private member into a publicly accessible member.

36 What is the output of the following code?

int x = 4;

int& r = x;

r += 3;

int y = r;

++y;

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

Reference variables Medium
A. 7 7 8
B. 8 8 8
C. 4 7 8
D. 7 8 8

37 A function must swap two caller variables and be invoked as swapValues(a, b). Which parameter declaration satisfies this requirement without returning the swapped values?

Differences between call by value, call by address and call by reference Medium
A. void swapValues(int* x, int* y)
B. void swapValues(int& x, int& y)
C. void swapValues(int x, int y)
D. void swapValues(const int& x, const int& y)

38 What does digitSum(5024) return?

int digitSum(int n) {

if (n == 0) return 0;

return n % 10 + digitSum(n / 10);

}

Recursion using functions and member functions Medium
A. 14
B. 9
C. 11
D. 7

39 What is printed by the following recursive member-function call?

class Power {

int base;

public:

Power(int b) : base(b) {}

int calculate(int n) const {

if (n == 0) return 1;

return base * calculate(n - 1);

}

};

Power p(3); cout << p.calculate(4);

Recursion using functions and member functions Medium
A. 81
B. 27
C. 12
D. 64

40 What happens when the following call is compiled?

void show(int);

void show(int, int = 0);

show(5);

Function overloading and scope rules Medium
A. The call is ambiguous.
B. show(int) is selected.
C. show(int, int) is selected.
D. Both overloads execute because the second overload supplies its missing argument automatically.

41 Assume C++17. What does the following program print?

CPP
#include <iostream>

struct Base {
    virtual int value() const { return 1; }
};

struct Derived : Base {
    int value() const override { return 2; }
};

int byValue(Base object) { return object.value(); }
int byReference(const Base& object) { return object.value(); }

int main() {
    Derived d;
    std::cout << byValue(d) << ' ' << byReference(d);
}

Introduction to concepts of OOP and OOP languages Hard
A. The program has undefined behavior because a derived object is passed as a base object.
B. 1 1
C. 1 2
D. 2 2

42 What does this program print?

CPP
#include <iostream>
#include <sstream>
#include <limits>

int main() {
    std::istringstream in("12x 34");
    int a = -1, b = -1;

    in >> a >> b;
    in.clear();
    in.ignore(std::numeric_limits<std::streamsize>::max(), ' ');
    in >> b;

    std::cout << a << ' ' << b << ' ' << std::boolalpha << in.fail();
}

Reading and writing data using cin and cout Hard
A. The second extraction repeatedly reads x, so the stream remains failed after clear().
B. 12 34 false
C. 12 -1 true
D. 12 0 false

43 Why is the declaration Holder h; ill-formed?

CPP
class Holder {
    int& value;
public:
    Holder() = default;
};

Holder h;

Creating classes Hard
A. A class containing a reference member cannot declare any constructor as default.
B. The reference member is private, so only a friend can initialize the object.
C. The defaulted constructor is deleted because the reference member has no initializer.
D. The compiler implicitly replaces the reference member with a pointer, but that pointer lacks an initializer.

44 Assume each constructor prints C followed by its identifier and each destructor prints D followed by its identifier. Which sequence is produced?

CPP
#include <iostream>

struct Trace {
    int id;
    Trace(int n) : id(n) { std::cout << 'C' << id << ' '; }
    ~Trace() { std::cout << 'D' << id << ' '; }
};

int main() {
    Trace a(1);
    {
        Trace b(2);
        static Trace c(3);
    }
    Trace d(4);
}

Class objects Hard
A. All three possible destruction orders are permitted because local and static objects have implementation-defined lifetimes.
B. C1 C2 C3 D3 D2 C4 D4 D1
C. C1 C2 C3 D2 C4 D4 D3 D1
D. C1 C2 C3 D2 C4 D4 D1 D3

45 Which labeled expression is ill-formed because of the special protected-access rule?

CPP
class Base {
protected:
    int x = 1;
};

class Derived : public Base {
public:
    int inspect(Base& b, Derived& d) {
        int p = x;        // I
        int q = this->x;  // II
        int r = d.x;      // III
        int s = b.x;      // IV
        return p + q + r + s;
    }
};

Accessing class members Hard
A. Expression IV
B. Expression II
C. Expression I
D. Expression III

46 Which statement correctly distinguishes C++ structures, unions, scoped enumerations, and classes?

Differences between structures, unions, enumerations and classes Hard
A. struct and class mainly differ in default access; a union overlays members; an enum class does not implicitly convert to an integer.
B. A struct cannot have virtual functions; a union can keep every member active; an enum class implicitly converts to int.
C. A class uniquely supports constructors, while structures, unions, and enumerations are restricted to plain aggregate data.
D. A struct always uses public inheritance; a union initializes all members; an enum class places enumerators in the surrounding scope.

47 A member function is defined inside a class definition placed in a header included by several translation units. Which statement is correct?

Inline and non-inline member functions Hard
A. The function has internal linkage, so each translation unit necessarily receives a distinct callable function.
B. The function is implicitly inline, so equivalent definitions may appear in multiple translation units.
C. The compiler must substitute the function body at every call site and is not allowed to emit an out-of-line definition.
D. The function violates the one-definition rule unless its declaration also contains the explicit keyword inline.

48 Assume C++17. What is printed?

CPP
#include <iostream>

class Counter {
    inline static int count = 0;
public:
    static void bump() { ++count; }
    static int value() { return count; }
};

int main() {
    Counter a, b;
    a.bump();
    b.bump();
    std::cout << a.value() << ' ' << Counter::value();
}

Static data members and static member functions Hard
A. 1 1
B. 1 2
C. 2 2
D. The calls through a and b operate on separate hidden copies, while the qualified call accesses the class-wide copy.

49 Consider a closed set of operations and an evolving set of shape types. Which comparison best describes the usual maintenance tradeoff between a procedural design using type-based dispatch and an object-oriented design using virtual methods?

Differences between procedural and object-oriented programming paradigms Hard
A. Virtual methods localize adding every operation, while type-based dispatch localizes adding every shape type.
B. Both designs localize both changes equally because dispatch placement does not affect module dependencies.
C. Virtual methods localize adding a shape type, while type-based dispatch often localizes adding an operation.
D. Type-based dispatch removes coupling entirely by placing all shape-specific behavior in one central procedure.

50 Immediately after the extraction below, which stream-state description is normally correct?

CPP
std::istringstream in("10");
int value;
in >> value;

Features of input/output streams Hard
A. eof() is true, fail() is false, and good() is false.
B. eof() is false, fail() is false, and good() is true.
C. eof() is true, fail() is true, and good() is false.
D. eof() remains false until another extraction is attempted, because successful formatted extraction never observes the end of the buffer.

51 Assume all declarations occur in the same namespace scope. What is the result?

CPP
#include <iostream>

void total(int a, int b = 2);
void total(int a = 1, int b);
void total(int a, int b) { std::cout << a + b; }

int main() {
    total();
}

Functions with default parameters or arguments Hard
A. The program is well-formed and prints 3.
B. The definition removes the earlier defaults, so the call with no arguments is invalid.
C. The call is ambiguous because each declaration contributes a distinct overload with a different default-argument set.
D. The second declaration is invalid because its second parameter has no default in that declaration.

52 An external-linkage inline function is identically defined in multiple translation units and contains a function-local static variable. Which statement is required by the C++ language?

Inline functions Hard
A. The local static denotes one shared object across all translation units.
B. Each translation unit receives a separate local static object.
C. The program is ill-formed because inline functions cannot contain static locals.
D. The number of local static objects depends on whether the optimizer actually substitutes the function body at each call site.

53 What does this program print?

CPP
#include <iostream>
#include <iomanip>

int main() {
    std::cout << std::hex
              << std::setfill('0')
              << std::setw(4) << 26
              << ' '
              << std::setw(2) << 10;
}

Manipulator functions Hard
A. 001a 10
B. 001a 000a
C. It prints 0026 10 because std::hex, std::setfill, and std::setw all reset after one formatted insertion.
D. 001a 0a

54 What does the first call print, and what does the second call print?

CPP
#include <iostream>

struct Base {
    void f(int) { std::cout << "Base"; }
};

struct Derived : Base {
    void f(double) { std::cout << "Derived"; }
};

int main() {
    Derived d;
    d.f(1);
    std::cout << ' ';

    struct Exposed : Derived {
        using Base::f;
    } e;
    e.f(1);
}

Function overloading and scope rules Hard
A. Derived Derived
B. The first call is ambiguous because both inherited and declared overloads participate automatically.
C. Derived Base
D. Base Base

55 Consider this hidden friend definition:

CPP
class Number {
    int value;
public:
    explicit Number(int v) : value(v) {}
    friend Number operator+(Number a, Number b) {
        return Number(a.value + b.value);
    }
};



Which use is ill-formed if no separate namespace-scope declaration of operator+ is provided?

Friend function and friend class Hard
A. Number c = Number(1) + Number(2);
B. Both call expressions are ill-formed because a friend defined inside a class is visible only to member functions of that class.
C. Number c = operator+(Number(1), Number(2));
D. auto pointer = &operator+;

56 Which declaration creates a reference that can be safely read in the following statement?

Reference variables Hard
A. auto&& r = std::move(3);, because an rvalue reference returned through std::move always extends temporary lifetime
B. const int& r = identity(3);, where identity returns its const int& parameter
C. const int& r = 1 + 2;
D. const int& r = std::max(1, 2);

57 What does this program print?

CPP
#include <iostream>

void modify(int value, int* address, int& reference) {
    value += 1;
    *address += 2;
    reference += 4;
}

int main() {
    int n = 1;
    modify(n, &n, n);
    std::cout << n;
}

Differences between call by value, call by address and call by reference Hard
A. 4
B. 7
C. The result is undefined because the pointer and reference aliases modify the same object within one function invocation.
D. 8

58 Why does this recursive function fail to reach its base case for an initial argument greater than zero?

CPP
unsigned countdown(unsigned n) {
    if (n == 0)
        return 0;
    return countdown(n--);
}

Recursion using functions and member functions Hard
A. Unsigned subtraction wraps immediately, so the first recursive call receives the maximum unsigned value.
B. Post-decrement passes the old value, so every recursive call receives the same positive number.
C. Tail-call optimization is mandatory here and removes the comparison with zero from all recursive invocations.
D. The decrement occurs only after the recursive call returns, but each call still receives n - 1.

59 Given the declarations below, which statement is correct?

CPP
class Vault {
    int code = 42;
    friend class Auditor;
};

class Auditor {
public:
    int read(const Vault& v) { return v.code; }
};

class SeniorAuditor : public Auditor {
public:
    int inspect(const Vault& v) { return v.code; }
};

Friend function and friend class Hard
A. Both member functions are valid because friendship is inherited by derived classes.
B. Auditor::read is valid, but SeniorAuditor::inspect is invalid.
C. Both member functions are invalid because friendship can be granted only to non-member functions.
D. SeniorAuditor::inspect is valid only because public inheritance transfers every private-access privilege from the base class.

60 A class declares void process(); in a header, and the function is defined outside the class in that same header without inline. The header is included by two translation units. What is the principal consequence?

Inline and non-inline member functions Hard
A. The program typically violates the one-definition rule at link time.
B. Each definition receives internal linkage because it appears in a header.
C. The compiler merges the definitions only when optimization is enabled, since linker deduplication is otherwise prohibited.
D. The function automatically becomes inline because its declaration is a class member.