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. Compilation
B. Tokenization
C. Iteration
D. Encapsulation

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

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

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

Reading and writing data using cin and cout Easy
A. cout >> age;
B. cin >> age;
C. cout << age;
D. cin << 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. cout >> "Hello";
D. cin << "Hello";

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

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

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

Class objects Easy
A. Student();
B. Student s;
C. object Student;
D. class 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. It stores only named constants
C. Its members share storage
D. Its members are always private

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 create recursive functions
C. To allocate shared storage
D. To overload stream operators

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 virtual function
B. A recursive function
C. An inline function
D. A friend 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. Two copies
D. One copy

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

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

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 function is deleted
B. The default value is used
C. The program always stops
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. static
B. friend
C. inline
D. extern

16 What does the endl manipulator normally do?

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

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. public
B. inline
C. friend
D. virtual

19 What is a reference variable in C++?

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

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 memory address
B. The argument's class definition
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. Multiple inheritance
B. Compile-time encapsulation
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. cin.ignore();
B. cin.sync();
C. cin.clear();
D. cout.flush();

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. 6
B. A garbage value
C. A compilation error because every class must provide an explicit default constructor
D. 0

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 5
B. 5 9
C. 9 9
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. d.Base::x = 8;
B. cout << Base::x;
C. cout << d.read();
D. cout << d.x;

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

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

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. class Calc { public: int square(int x) { return x * x; } };
B. int inline Calc::square(int x) { return x * x; }
C. int Calc::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. Store the balance privately and modify it through public member functions.
D. Pass the balance by value to every withdrawal procedure.

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. The integer extraction fails because the complete input token is not a valid integer
B. number is 17 and letter is 'a'
C. number is 17 and letter is 'c'
D. number is 0 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 = 3);
C. void process(int a, int b = 2, int c);
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. An inline function is automatically executed before main() because its body is processed during compilation.
B. The compiler may choose not to expand the function at the call site.
C. An inline function cannot contain loops, local variables, or conditional statements.
D. The compiler must replace every function call with the complete function body.

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 05
B. 0007 005
C. 7000 50
D. 0007 5

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. Derived::show(double)
B. Neither function, because an integer cannot be implicitly converted to a double in an overloaded member call
C. Base::show(int)
D. Both functions, in declaration order

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. All non-member functions may read private members of constant objects.
B. Passing Vault by reference automatically changes every private member into a publicly accessible member.
C. reveal() is explicitly declared as a friend of Vault.
D. reveal() becomes an inherited member function of Vault.

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. 4 7 8
B. 8 8 8
C. 7 8 8
D. 7 7 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(const int& x, const int& y)
B. void swapValues(int x, int y)
C. void swapValues(int& x, int& y)
D. void swapValues(int* x, 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. 7
B. 9
C. 14
D. 11

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. 12
B. 81
C. 27
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. Both overloads execute because the second overload supplies its missing argument automatically.
B. show(int, int) is selected.
C. show(int) is selected.
D. The call is ambiguous.

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. 2 2
B. 1 1
C. The program has undefined behavior because a derived object is passed as a base object.
D. 1 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. 12 -1 true
B. The second extraction repeatedly reads x, so the stream remains failed after clear().
C. 12 0 false
D. 12 34 false

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

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

Holder h;

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

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. C1 C2 C3 D3 D2 C4 D4 D1
B. C1 C2 C3 D2 C4 D4 D3 D1
C. C1 C2 C3 D2 C4 D4 D1 D3
D. All three possible destruction orders are permitted because local and static objects have implementation-defined lifetimes.

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 III
D. Expression I

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

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

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 is implicitly inline, so equivalent definitions may appear in multiple translation units.
B. The function violates the one-definition rule unless its declaration also contains the explicit keyword inline.
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 has internal linkage, so each translation unit necessarily receives a distinct callable function.

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 2
B. The calls through a and b operate on separate hidden copies, while the qualified call accesses the class-wide copy.
C. 2 2
D. 1 1

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. Type-based dispatch removes coupling entirely by placing all shape-specific behavior in one central procedure.
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. Virtual methods localize adding every operation, while type-based dispatch localizes adding every shape type.

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 true, and good() is false.
B. eof() remains false until another extraction is attempted, because successful formatted extraction never observes the end of the buffer.
C. eof() is true, fail() is false, and good() is false.
D. eof() is false, fail() is false, and good() is true.

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 definition removes the earlier defaults, so the call with no arguments is invalid.
B. The call is ambiguous because each declaration contributes a distinct overload with a different default-argument set.
C. The second declaration is invalid because its second parameter has no default in that declaration.
D. The program is well-formed and prints 3.

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. The number of local static objects depends on whether the optimizer actually substitutes the function body at each call site.
C. The program is ill-formed because inline functions cannot contain static locals.
D. Each translation unit receives a separate local static object.

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. It prints 0026 10 because std::hex, std::setfill, and std::setw all reset after one formatted insertion.
B. 001a 10
C. 001a 000a
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. Base Base
C. Derived Base
D. The first call is ambiguous because both inherited and declared overloads participate automatically.

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 = operator+(Number(1), Number(2));
B. auto pointer = &operator+;
C. Both call expressions are ill-formed because a friend defined inside a class is visible only to member functions of that class.
D. Number c = Number(1) + Number(2);

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. 7
B. 8
C. The result is undefined because the pointer and reference aliases modify the same object within one function invocation.
D. 4

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 invalid because friendship can be granted only to non-member functions.
B. Auditor::read is valid, but SeniorAuditor::inspect is invalid.
C. Both member functions are valid because friendship is inherited by derived classes.
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 compiler merges the definitions only when optimization is enabled, since linker deduplication is otherwise prohibited.
B. Each definition receives internal linkage because it appears in a header.
C. The function automatically becomes inline because its declaration is a class member.
D. The program typically violates the one-definition rule at link time.