Explore topic-wise InterviewSolutions in .

This section includes InterviewSolutions, each offering curated multiple-choice questions to sharpen your knowledge and support exam preparation. Choose a topic below to get started.

1.

What will be the output in below code?

Answer» PUBLIC class Demo{ public static void main(STRING[] arr){ System.out.println(“Main1”); } public static void main(String arr){ System.out.println(“Main2”); } }

Output:

Main1

Reason:
Here the main() method is overloaded. But JVM only understands the main method which has a String[] ARGUMENT in its definition. Hence Main1 is PRINTED and the overloaded main method is IGNORED

2.

Predict the output?

Answer»

#include&LT;iostream> using NAMESPACE std; class ClassA { public: ClassA(int ii = 0) : i(ii) {} void show() { cout << "i = " << i << ENDL;} private: int i; }; class ClassB { public: ClassB(int xx) : x(xx) {} operator ClassA() const { return ClassA(x); } private: int x; }; void g(ClassA a) { a.show(); } int main() { ClassB b(10); g(b); g(20); getchar(); return 0; }

Output:

i = 10i = 20

Reason:
ClassA contains a conversion constructor. Due to this, the OBJECTS of ClassA can have integer values. So the STATEMENT g(20) works. Also, ClassB has a conversion operator overloaded. So the statement g(b) also works.

3.

What will be the output of the below code?

Answer»

class SCALER{ static INT i; static { System.out.println(“a”); i = 100; }}public class StaticBlock{ static { System.out.println(“b”); } public static void MAIN(String[] ARGS) { System.out.println(“c”); System.out.println(Scaler.i); }}

Output:

bca100

Reason:
FIRSTLY the static block inside the main-method calling class will be implemented. Hence ‘b’ will be printed first. Then the main method is called, and now the sequence is kept as expected.

4.

What is the output of the below code?

Answer»

#include<iostream> using namespace std; class BaseClass1 { public: BaseClass1() { cout << " BaseClass1 constructor called" << endl; } }; class BaseClass2 { public: BaseClass2() { cout << "BaseClass2 constructor called" << endl; } }; class DERIVEDCLASS: public BaseClass1, public BaseClass2 { public: DerivedClass() { cout << "DerivedClass constructor called" << endl; } }; int main() { DerivedClass derived_class; return 0; }

Output:

BaseClass1 constructor calledBaseClass2 constructor calledDerivedClass constructor called

Reason:
The above program demonstrates Multiple INHERITANCES. So when the Derived class’s constructor is called, it automatically calls the Base class's CONSTRUCTORS from left to right ORDER of INHERITANCE.