OOP Quiz
OOP Quiz
Question 1
Java
class Point {
int m_x, m_y;
public Point(int x, int y) { m_x = x; m_y = y; }
public static void main(String args[])
{
Point p = new Point();
}
}
Yes
No
Discuss it
Question 1 ‒ Explanation
The main function calls parameterless constructor, but there is only one constructor defined in class which takes two
parameters. Note that if we write our own constructor, then compiler doesn\'t create default constructor in Java. Thi
s behavior is same as C++.
Question 2
JAVA
class Test
{
static int a;
static
{
a = 4;
System.out.println ("inside static block\\n");
System.out.println ("a = " + a);
}
Test()
{
System.out.println ("\\ninside constructor\\n");
a = 10;
}
}
}
Compiler Error
Discuss it
Question 2 ‒ Explanation
Static blocks are called before constructors. Therefore, on object creation of class Test, static block is called. So, s
tatic variable a = 4. Then constructor Test() is called which assigns a = 10. Finally, function func() increments its v
alue.
Question 3
Java
(10.0 + 15.0i)
(0.0 + 0.0i)
Discuss it
Question 4
Java
class Point {
int m_x, m_y;
10
compiler error
Discuss it
Question 4 ‒ Explanation
it\'s a simple program where constructor is called with parameters and values are initialized.
Question 5
1, 2
1, 2 and 3
1, 2, 3 and 4
Discuss it
Question 6
Java
package main;
class T {
int t = 20;
}
class Main {
public static void main(String args[]) {
T t1 = new T();
System.out.println(t1.t);
}
}
20
Compiler Error
Discuss it
Question 6 ‒ Explanation
In Java, member variables can assigned a value with declaration. In C++, only static const variables can be assign
ed like this.
Question 7
Java
class T {
int t = 20;
T() {
t = 40;
}
}
class Main {
public static void main(String args[]) {
T t1 = new T();
System.out.println(t1.t);
}
}
20
40
Compiler Error
Discuss it
Question 7 ‒ Explanation
The values assigned inside constructor overwrite the values initialized with declaration.
1, 2 and 4
Only 1 and 2
1, 2 and 3
2, 3 and 4
Discuss it
Question 1 ‒ Explanation
See http://www.geeksforgeeks.org/can-override-private-methods-java/ and htt
p://www.geeksforgeeks.org/comparison-of-inheritance-in-c-and-java/
Question 2
Java
class Base {
public void Print() {
System.out.println("Base");
}
}
class Main{
public static void DoPrint( Base o ) {
o.Print();
}
public static void main(String[] args) {
Base x = new Base();
Base y = new Derived();
Derived z = new Derived();
DoPrint(x);
DoPrint(y);
DoPrint(z);
}
}
Base
Derived
Derived
Base
Base
Derived
Base
Derived
Base
Compiler Error
Discuss it
Question 2 ‒ Explanation
See question 1 of
http://www.geeksforgeeks.org/output-of-java-program-set-2/
Question 3
Predict the output of following program. Note that fun() is public in base and
private in derived.
Java
class Base {
public void foo() { System.out.println("Base"); }
}
Base
Derived
Compiler Error
Runtime Error
Discuss it
Question 3 ‒ Explanation
It is compiler error to give more restrictive access to a derived class function w
hich overrides a base class function.
Question 4
Java
// filename Main.java
class Grandparent {
public void Print() {
System.out.println("Grandparent\'s Print()");
}
}
Grandparent\'s Print()
Parent\'s Print()
Child\'s Print()
Runtime Error
Discuss it
Question 4 ‒ Explanation
In Java, it is not allowed to do super.super. We can only access Grandparent\'s
members using Parent. For example, the following program works fine.
class Main {
public static void main(String[] args) {
Child c = new Child();
c.Print();
}
}
Question 5
Java
class Base {
public void show() {
System.out.println("Base::show() called");
}
}
Derived::show() called
Base::show() called
Discuss it
Question 5 ‒ Explanation
In the above program, b is a reference of Base type and refers to an abject of De
rived class. In Java, functions are virtual by default. So the run time polymorph
ism happens and derived fun() is called.
Question 6
Which of the following is true about inheritance in Java. 1) In Java all classes
inherit from the Object class directly or indirectly. The Object class is root of all
classes. 2) Multiple inheritance is not allowed in Java. 3) Unlike C++, there is
nothing like type of inheritance in Java where we can specify whether the
inheritance is protected, public or private.
1, 2 and 3
1 and 2
2 and 3
1 and 3
Discuss it
Question 6 ‒ Explanation
See Comparison of Inheritance in C++ and Java
Question 7
class Main {
public static void main(String args[])
{
Complex c = new Complex(10, 15);
System.out.println("Complex number is " + c);
}
}
Discuss it
Question 7 ‒ Explanation
See http://www.geeksforgeeks.org/overriding-tostring-method-in-java/
Question 8
Java
class Base {
final public void show() {
System.out.println("Base::show() called");
}
}
class Main {
public static void main(String[] args) {
Base b = new Derived();;
b.show();
}
}
Base::show() called
Derived::show() called
Compiler Error
Runtime Error
Discuss it
Question 8 ‒ Explanation
Final methods cannot be overridden. See the compiler error
here
.
Question 9
\Java\
class Base {
public static void show() {
System.out.println("Base::show() called");
}
}
class Main {
public static void main(String[] args) {
Base b = new Derived();
b.show();
}
}
Base::show() called
Derived::show() called
Compiler Error
Discuss it
Question 9 ‒ Explanation
Like C++, when a function is static, runtime polymorphism doesn\'t happen.
Neither I nor II
Both I and II
Discuss it
Question 1 ‒ Explanation
Abstract data class is not used to create objects in Java and it is design
ed only to act as a base class to be inherited by other classes. Both Stat
ement are correct. For more information Refer:Abstract Classes in Java
Option (D) is correct.
Question 2
Discuss it
Question 2 ‒ Explanation
An abstract class is a class that is designed to be specifically used as
a base class. An abstract class contains at least one pure virtual funct
ion. A pure virtual function can be declared by using a pure specifier
( = 0 ) in the declaration of a virtual member function in the class de
claration. Option (C) is correct.
Question 3
Discuss it
Question 3 ‒ Explanation
Making atleast one member function as pure virtual function is the met
hod to make abstract class. For more information on Abstract Class Ref
er:Pure Virtual Functions and Abstract Classes in C++ Option (A) is corre
ct.
Question 4
which translates JDBC function calls into API not native to DBMS
Discuss it
Question 4 ‒ Explanation
JDBC type 4 driver,works directly by connecting to the database serv
er through socket connections and converts JDBC calls to vendor-sp
ecific database protocols.These drivers don\'t require any intermedia
te layer. So, option (C) is correct.
Question 5
Predict the output of the following program.
Java
a = 10
a = 20
Compilation error
Discuss it
Question 5 ‒ Explanation
Final method can’t be overridden. Thus, an abstract function can’t b
e final.
Question 6
Discuss it
Question 7
1, 3 and 4
1, 2 and 4
2, 3 and 4
1, 2, 3 and 4
Discuss it
Question 7 ‒ Explanation
Answer: (A)
garbage value
compiler error
runtime error
Discuss it
Question 1 ‒ Explanation
t is just a reference, the object referred by t is not allocated any memory. Unlike C++, in Java all
non-primitive objects must be explicitly allocated and these objects are allocated on heap. The
following is corrected program.
Java
class Test {
int i;
}
class Main {
public static void main(String args[]) {
Test t = new Test();
System.out.println(t.i);
}
Question 2
JAVA
class demo
{
int a, b;
demo()
{
a = 10;
b = 20;
}
class Test
{
obj1.a += 1;
obj1.b += 1;
}
}
Compile error
values of obj1:
a = 11 b = 21
values of obj2:
a = 11 b = 21
values of obj1:
a = 11 b = 21
values of obj2:
a = 10 b = 20
values of obj1:
a = 11 b = 20
values of obj2:
a = 10 b = 21
Discuss it
Question 2 ‒ Explanation
Assignment of obj2 to obj1 makes obj2 a reference to obj1. Therefore, any change in obj1 will
be reflected in obj2 also.
Question 3
Java
class demoClass
{
int a = 1;
void func()
{
demo obj = new demo();
obj.display();
}
class demo
{
int b = 2;
void display()
{
System.out.println("\\na = " + a);
}
}
void get()
{
System.out.println("\\nb = " + b);
}
}
class Test
{
public static void main(String[] args)
{
demoClass obj = new demoClass();
obj.func();
obj.get();
}
}
a = 1
b = 2
Compilation error
b = 2
a = 1
Discuss it
Question 3 ‒ Explanation
Members of inner class ‘demo’ can not be used in the outer class ‘Test’. Thus, get() of outer clas
s can not access variable ‘b’ of inner class.
Question 4
Java
class First
{
void display()
{
System.out.println("Inside First");
}
}
void display()
{
System.out.println("Inside Second");
}
}
class Test
{
First ref;
ref = obj1;
ref.display();
ref = obj2;
ref.display();
}
}
Compilation error
Inside First
Inside Second
Inside First
Inside First
Runtime error
Discuss it
Question 4 ‒ Explanation
‘ref’ is a reference variable which obtains the reference of object of class First and calls its functi
on display(). Then ‘ref’ refers to object of class Second and calls its function display().
Question 5
Java
class Test
{
int a = 1;
int b = 2;
}
}
obj1.a = 1 obj1.b = 2
obj2.a = 4 obj2.b = 3
obj1.a = 4 obj1.b = 3
obj2.a = 4 obj2.b = 3
Compilation error
Discuss it
Question 5 ‒ Explanation
Question 6
Java
class Test
{
int a = 1;
int b = 2;
}
}
obj1.a = 1 obj1.b = 2
obj2.a = 4 obj2.b = 3
obj1.a = 4 obj1.b = 3
obj2.a = 4 obj2.b = 3
Compilation error
Discuss it
Question 6 ‒ Explanation
Question 7
What is the output of the following JAVA program? public class Good{ Private int m; Public Good(int
m){this.m=m;} public Boolean equals(Good n){return n.m=m;} public static void main(String args [ ]){
Good m1=new Good(22); Good m2=new Good(22); Object S1=new Good(22); Object S2=new
good(22); System.out.println(m1.equals(m2)); System.out.println(m1.equals(s2));
System.out.println(m1.equals(s2)); System.out.println(s1.equals(m2)); } }
Discuss it
Question 7 ‒ Explanation
Note: Question is wrong. First change boolean to int data type then there is a possibility of opti
on 4 is correct. So marks will be added to all students. GeeksforGeeks code link: https://ide.gee
ksforgeeks.org/tWbTXjT2VA Output: true false false false
Question 8
class simple
{
public static void main(String[ ] args)
{
simple obj = new simple( );
obj.start( );
}
void start( )
{
long [ ] P= {3, 4, 5};
long [ ] Q= method (P);
System.out.print (P[0] + P[1] + P[2]+”:”);
System.out.print (Q[0] + Q[1] + Q[2]);
}
long [ ] method (long [ ] R)
{
R [1]=7;
return R;
}
} //end of class
12 : 15
15 : 12
12 : 12
15 : 15
Discuss it
Question 8 ‒ Explanation
When above program compliled and run on ide then it will produce 15:15. IDE link Option (D) is cor
rect.
Question 9
Java
Class Test {
public static void main(String[] args) {
Test obj = new Test();
obj.start();
}
void start() {
String stra = ”do”;
String strb = method(stra);
System.out.print(“: ”+stra + strb);
}
String method(String stra) {
stra = stra + ”good”;
System.out.print(stra);
return“ good”;
}
}
dogood : dogoodgood
dogood : gooddogood
dogood : dodogood
dogood : dogood
Discuss it
Question 10
Symmetric
Asymmetric
Synchronous
Asynchronous
Discuss it
Question 10 ‒ Explanation
Java uses threads to enable the entire environment to be asynchronous. Asynchronous threadi
ng is pre-emptive i.e. a thread once start executing a task it can hold it in mid, save the current
state and start executing another task (context switching) according to priority and other specif
ied criteria and threading. So, option (D) is correct.
1 2
Friend
Discuss it
Question 11 ‒ Explanation
In Java, when we implement an interface method, it must be declared
as Public. For more information on Java interface Refer:Interfaces in Ja
va option (C) is correct.
Question 12
Java
class Test {
int i;
}
class Main {
public static void main(String args[]) {
Test t = new Test();
System.out.println(t.i);
}
}
garbage value
compiler error
runtime error
Discuss it
Question 12 ‒ Explanation
In Java, fields of classes and objects that do not have an explicit initi
alizer and elements of arrays are automatically initialized with the de
fault value for their type (false for boolean, 0 for all numerical types,
null for all reference types). Local variables in Java must be definitely
assigned to before they are accessed, or it is a compile error.
1 2
Question 1
Discuss it
Question 1 ‒ Explanation
See final in Java.
Question 2
Java
Trending Now Data Structures Algorithms System Design Foundational Courses Data Science Practice Problem Python Machine L
class Main {
public static void main(String args[]){
final int i;
i = 20;
System.out.println(i);
}
}
20
Compiler Error
Garbage value
Discuss it
Question 2 ‒ Explanation
There is no error in the program. final variables can be assigned value only once. In the above program, i is ass
igned a value as 20, so 20 is printed.
Question 3
Java
class Main {
public static void main(String args[]){
final int i;
i = 20;
i = 30;
System.out.println(i);
}
}
30
Compiler Error
Garbage value
Discuss it
Question 3 ‒ Explanation
i is assigned a value twice. Final variables can be assigned values only one. Following is the compiler error "Mai
n.java:5: error: variable i might already have been assigned"
Question 4
Java
class Base {
public final void show() {
System.out.println("Base::show() called");
}
}
class Derived extends Base {
public void show() {
System.out.println("Derived::show() called");
}
}
public class Main {
public static void main(String[] args) {
Base b = new Derived();;
b.show();
}
}
Derived::show() called
Base::show() called
Compiler Error
Exception
Discuss it
Question 4 ‒ Explanation
compiler error: show() in Derived cannot override show() in Base
garbage value
compiler error
runtime error
Discuss it
Question 1 ‒ Explanation
Unlike class members, local variables of methods must be assigne
d a value to before they are accessed, or it is a compile error.
Question 2
class Test {
public static void main(String[] args) {
for(int i = 0; 0; i++)
{
System.out.println("Hello");
break;
}
}
}
Hello
Empty Output
Compiler error
Runtime error
Discuss it
Question 2 ‒ Explanation
The error is in for loop where 0 is used in place of boolean value.
Unlike C++, use of non boolean variables in place of bool is not all
owed
Question 3
Java
class Test
{
public static void main(String[] args)
{
Double object = new Double("2.4");
int a = object.intValue();
byte b = object.byteValue();
float d = object.floatValue();
double c = object.doubleValue();
System.out.println(a + b + c + d );
}
}
8.8
8.800000095367432
Discuss it
Question 3 ‒ Explanation
Arithmetic conversions are implicitly performed to cast the values
to a common type. The compiler first performs integer promotion.
If the operands still have different types, then they are converted t
o the type that appears highest in the hierarchy.
Question 4
Discuss it
Question 4 ‒ Explanation
In JAVA, constant are not declared using \'static\' keyword and a cl
ass can implement multiple interfaces but class can inherit one cla
ss only. So, option (B) is correct.
0
0
garbage value
garbage value
Compiler Error
Exception
Discuss it
Question 1 ‒ Explanation
In Java, it is not allowed to put the size of the array in the declaration because an array declarat
ion specifies only the element type and the variable name. The size is specified when you alloca
te space for the array. Even the following simple program won\'t compile.
class Test {
public static void main(String args[]) {
int arr[5]; //Error
}
}
Question 2
Java
import java.util.Arrays;
class Test
{
public static void main (String[] args)
{
int arr1[] = {1, 2, 3};
int arr2[] = {1, 2, 3};
if (Arrays.equals(arr1, arr2))
System.out.println("Same");
else
System.out.println("Not same");
}
}
Same
Not Same
Discuss it
Question 2 ‒ Explanation
See http://www.geeksforgeeks.org/compare-two-arrays-java/
Question 3
Consider the following C program which is supposed to compute the transpose of a given 4 x 4
matrix M. Note that, there is an X in the program which indicates some missing statements. Choose
the correct option to replace X in the program.
#include<stdio.h>
#define ROW 4
#define COL 4
int M[ROW][COL] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16};
main()
{
int i, j, t;
for (i = 0; i < 4; ++i)
{
X
}
for (1 = 0; i < 4; ++i)
for (j = 0; j < 4; ++j)
printf ("%d", M[i][j]);
}
Discuss it
Question 3 ‒ Explanation
To compute transpose j needs to be started with i,so A and B are WRONG
In D, given statement is wrong as temporary variable t needs to be assigned some value and N
OT vice versa
M[i][j] = t;
Question 4
class Test
{
public static void main (String[] args)
{
int arr1[] = {1, 2, 3};
int arr2[] = {1, 2, 3};
if (arr1 == arr2)
System.out.println("Same");
else
System.out.println("Not same");
}
}
Same
Not Same
Discuss it
Question 4 ‒ Explanation
See http://www.geeksforgeeks.org/compare-two-arrays-java/
Question 5
Java
class Test {
public static void main(String args[]) {
int arr[] = new int[2];
System.out.println(arr[0]);
System.out.println(arr[1]);
}
}
0
0
garbage value
garbage value
Compiler Error
Exception
Discuss it
Question 5 ‒ Explanation
Java arrays are first class objects and all members of objects are initialized with default values li
ke o, null.
Question 6
Java
class Test
{
public static void main (String[] args)
{
int arr1[] = {1, 2, 3};
int arr2[] = {1, 2, 3};
if (arr1.equals(arr2))
System.out.println("Same");
else
System.out.println("Not same");
}
}
Same
Not same
Discuss it
Question 6 ‒ Explanation
arr1.equals(arr2) is same as (arr1 == arr2)
Question 7
Java
10 20 30 40 50
Compiler Error
10 20 30 40
Discuss it
Question 7 ‒ Explanation
It is a simple program where an array is first created then traversed. The important thing to not
e is, unlike C++, arrays are first class objects in Java. For example, in the following program, siz
e of array is accessed using length which is a member of arr[] object.
Question 8
Discuss it
Question 8 ‒ Explanation
In Java, arrays are objects, they have members like length. The length member is final and cannot
be changed. All objects are allocated on heap in Java, so arrays are also allocated on heap.
Question 9
int i, j, k = 0;
for (i = 0; i < 4; i++) {
for (j = 0; j < i + 1; j++) {
arr[i][j] = k;
k++;
}
}
for (i = 0; i < 4; i++) {
for (j = 0; j < i + 1; j++) {
System.out.print(" " + arr[i][j]);
k++;
}
System.out.println();
}
}
}
Compiler Error
0
1 2
3 4 5
6 7 8 9
0
0 0
0 0 0
0 0 0 0
9
7 8
4 5 6
0 1 2 3
Discuss it
Question 9 ‒ Explanation
In Java, we can create jagged arrays. Refer Jagged Array in Java for details.