OOP MCQs - Abstract Classes, Interfaces, Static, Friend and Operators (Set 5)

OOP MCQs - Abstract Classes, Interfaces, Static, Friend and Operators (Set 5) - CodingHumans

This set brings together several features that exam papers love to mix: abstract classes and pure virtual functions, Java interfaces with default methods, static data and functions, friend functions and classes, the this pointer and C++ operator overloading, including prefix versus postfix increment. The output questions are typical of university semester exams, GATE and UGC NET practice and C++ or Java interview rounds.

How to practise: read each question and tap the option you think is correct. You will see right away whether you got it, along with a short explanation, and your score is tracked at the bottom of the screen. This set has 18 questions.

Object Oriented Programming MCQ series: Set 1 | Set 2 | Set 3 | Set 4 | Set 5 | Set 6

Topics covered in this set

  • Abstract classes and pure virtual functions
  • Java interfaces and default methods
  • Static data members and static functions
  • Friend functions and friend classes
  • The this pointer and method chaining
  • Operator overloading rules
  • Prefix vs postfix increment overloading

Object Oriented Programming MCQs with answers and explanations

Q1. How is a class made abstract in C++?

a) By declaring it with the abstract keyword

b) By making all its constructors private

c) By declaring at least one pure virtual function, such as virtual void draw() = 0;

d) By omitting a destructor

Q2. What does this Java program print?

abstract class Shape {
    Shape() { System.out.print("S"); }
    abstract double area();
}
class Sq extends Shape {
    Sq() { System.out.print("Q"); }
    double area() { return 4; }
}
public class Main {
    public static void main(String[] args) {
        Shape s = new Sq();
        System.out.print(s.area());
    }
}

a) Q4.0

b) SQ4.0

c) SQ4

d) Compilation error: an abstract class cannot have a constructor

Q3. Variables declared in a Java interface are implicitly:

a) public, static and final

b) private and final

c) protected and static

d) public instance variables

Q4. What does this Java program print?

interface X { default String hi() { return "X"; } }
interface Y { default String hi() { return "Y"; } }
class Z implements X, Y {
    public String hi() { return X.super.hi() + Y.super.hi(); }
}
public class Main {
    public static void main(String[] args) {
        System.out.print(new Z().hi());
    }
}

a) X

b) Y

c) XY

d) Compilation error

Q5. Which statement about Java abstract classes and interfaces is correct?

a) An interface can declare constructors that implementing classes must call

b) A class can extend several abstract classes but implement only one interface

c) Abstract classes cannot contain any concrete methods

d) An abstract class can have constructors and instance fields, while an interface cannot have instance fields

Q6. What does this Java program print?

class C {
    static int s = 0;
    int i = 0;
    C() { s++; i++; }
}
public class Main {
    public static void main(String[] args) {
        new C(); new C();
        C c = new C();
        System.out.print(c.s + " " + c.i);
    }
}

a) 3 3

b) 1 1

c) 3 1

d) 1 3

Q7. Why can a static member function in C++ not access non-static data members directly?

a) It has no this pointer, because it is not called on a particular object

b) Static functions are compiled in a separate translation unit

c) Non-static members are always private

d) Static functions can only return void

Q8. A C++ class declares static int count; inside its body (no inline, pre-C++17 style). What else is needed if count is used?

a) Nothing; the declaration inside the class also defines it

b) A single definition at namespace scope, such as int Widget::count = 0;

c) Declaring count as mutable

d) Initialising count in every constructor

Q9. What is printed by this C++ program?

#include <iostream>
using namespace std;
class Counter {
    int c;
public:
    Counter() : c(0) {}
    void inc() { c++; }
    int get() const { return c; }
    friend void reset(Counter&);
};
void reset(Counter& k) { k.c = 100; }
int main() {
    Counter k;
    k.inc();
    reset(k);
    k.inc();
    cout << k.get();
}

a) 1

b) 100

c) 101

d) 2

Q10. Class A declares class B as its friend. Which statement is correct?

a) A can now access B's private members as well

b) Classes derived from B also become friends of A

c) Any friend of B automatically becomes a friend of A

d) Member functions of B can access A's private members, but not the other way round unless B also declares A a friend

Q11. Inside a const member function of class Box, what is the type of the this pointer?

a) Box*

b) const Box* (pointer to a const Box)

c) Box&

d) const Box&

Q12. What is printed by this C++ program?

#include <iostream>
using namespace std;
struct S {
    int v = 0;
    S add(int k) { v += k; return *this; }
};
int main() {
    S s;
    s.add(2).add(3);
    cout << s.v;
}

a) 2

b) 5

c) 3

d) 0

Q13. What is printed by this C++ program?

#include <iostream>
using namespace std;
struct V {
    int x;
    V(int a) : x(a) {}
    V operator+(const V& o) const { return V(x + o.x * 2); }
};
int main() {
    V a(1), b(2), c(3);
    cout << (a + b + c).x;
}

a) 11

b) 6

c) 17

d) 14

Q14. What is printed by this C++ program?

#include <iostream>
using namespace std;
struct N {
    int v;
    N(int a) : v(a) {}
    N& operator++() { ++v; return *this; }
    N operator++(int) { N t = *this; v += 10; return t; }
};
int main() {
    N n(1);
    N a = n++;
    N b = ++n;
    cout << a.v << " " << b.v << " " << n.v;
}

a) 2 12 12

b) 1 12 12

c) 1 11 12

d) 11 12 12

Q15. Which C++ operator cannot be overloaded?

a) []

b) ->

c) ?:

d) ()

Q16. Why is operator<< for printing a class to an ostream normally written as a non-member (often friend) function?

a) Because the left operand is the ostream, not an object of the class, so it cannot be a member of the class

b) Because member operators cannot return references

c) Because operator<< must always be static

d) Because friend functions are faster than member functions

Q17. How does C++ tell apart the overloads for prefix ++x and postfix x++?

a) The postfix version must be a friend function

b) The prefix version returns void

c) They are distinguished by their return types

d) The postfix version takes an extra unused int parameter

Q18. A concrete Java class implements an interface but does not provide a body for one of its abstract methods. What must be done for the code to compile?

a) Mark the method static in the interface

b) Nothing; the JVM supplies an empty body at run time

c) Declare the method private in the class

d) Declare the class abstract or implement the missing method

Quick revision notes

  • A C++ class with any pure virtual function (= 0) is abstract.
  • Abstract classes can have constructors; they run when a subclass is created.
  • Interface fields are public static final; methods may be abstract, default, static or private.
  • Static members are shared by all objects; static functions have no this pointer.
  • Friendship is one-way, not inherited and not transitive.
  • Return *this by reference to support method chaining.
  • Cannot overload: ::, ., .*, ?:, sizeof.
  • Postfix operator++ takes a dummy int parameter and returns the old value.

Frequently asked questions

When should I use an abstract class instead of an interface?

Use an abstract class when related classes share state or constructor logic and form a true IS-A hierarchy. Use an interface to describe a capability that unrelated classes can implement, especially since a class can implement many interfaces.

Do friend functions break encapsulation?

They widen access, but only for functions the class itself chooses to trust, so the class stays in control. They are commonly used for operators like << that need private data but cannot be members.

What is the this pointer?

this is an implicit pointer available inside non-static member functions that points to the object the function was called on. It is used to resolve name clashes between members and parameters and to return the object for chaining.

Comments

Answered 0/0Correct 0