Java Output

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 70

1.

What will be the output of the program?


class Output
{
final static short i = 2; i এর value পুরা programme always একই থাকব ্
public static int j = 0;

public static void main(String [] args)


{
for (int k = 0; k < 3; k++)
{
switch (k)
{
case i: System.out.print(" 0 ");
case i-1: System.out.print(" 1 ");
case i-2: System.out.print(" 2 ");
}
}
}
}

A
210100

B
012122

C
212012

D
012

E
Compilation Error
2.
What will be the output of the program?
class Output
{
final static short i = 2;
public static int j = 0;

public static void main(String [] args)


{
for (int k = 0; k < 3; k++)
{
switch (k)
{
case i : System.out.print(" 1 ");
case i - 1 : System.out.print(" 2 ");
default : System.out.print(" 0 ");
}
}
}
}
A 012

B 012020

C 210100

D 020120

E Compilation Error
3.
What will be the output of the following program?
public class Final {
int lanif = 37;
int nafi = 21;
public static void main(String[] args) {
final Final f = new Final();
f.process2();
Final f2 = modify(f);
f2.process2();
}
public static final Final modify(final Final f) {
f.process();
Final f2 = new Final();
f2.process();
return f2;
}
final void process() {
lanif = nafi + nafi;
System.out.print(lanif + " " + nafi + " ");
}
void process2() {
nafi = lanif / 2;
System.out.print(nafi + " " + lanif + " ");
}
}

A 42 21 21 42 18 37 36 18

B 18 37 36 18 18 37 36 18

C 18 37 36 18 42 21 21 42

D Compilation Error
4.
Which of the following statements is true? No, An abstract class can’t be final because the final
and abstract are opposite terms in JAVA.
M: Abstract class can be final Reason: An abstract class must be inherited by any
N: We can create an object of an abstract class derived class because a derived class is responsible to
provide the implementation of abstract methods of an
abstract class. But on another hand, if a class is a
final class, then it can’t be extended(inherited). So
A M is true, N is false both concepts are opposite to each other.

No, we can't create an object of an abstract class. But we can


B N is true, M is false create a reference variable of an abstract class. The reference
variable is used to refer to the objects of derived classes
C Both of them are true (subclasses of abstract class).

D Both of them are false


5.
What will be the output of the following program?
public class Final
{
final int assign = 30;

public static void main(String[] args)


{
final int result = 20;
final int assign;

Final f = new Final();


assign = 20;

System.out.println(assign);
System.out.println(f.assign);
System.out.println(f.process(result));
}

int process(int a)
{
return a + 5;
}
}

20
A 20
25

20
B 30
25
C Compilation Error

D Runtime Error
6.
What will be the output of the following program?
public class Final
{
public static void main(String[] args)
{
final int result;
result = 20;

int assign = process(result);


result = assign;

System.out.println(result);
}

static int process(int a)


{
return a + 5;
}
}

A 20

B 25

C Compilation Error

D Runtime Error
7.
What will be the output of the following program?
public class Final
{
final int assign;

public static void main(String[] args)


{
final int result = 20;

Final f = new Final();


f.assign = process(result);
System.out.println(f.assign);
}

final static int process(int a)


{
return a + 5;
}
}

A 20

B 25

C Compilation Error

D Runtime Error
8.
What will be the output of the following program?
public class Final
{
int a = 30;

public static void main(String[] args)


{
final int assign;

Final b = new Final(); new obj b created


process(b); send b obj to process function ....programme jumps to procss and add 5 to b.a and return it
System.out.println(b.a); b.a =35 printed

process(b); send b obj to process function ....programme jumps to procss and add 5 to b.a and return it
assign = b.a; b.a value is assigned to a variable named assign......here now assign = 40
System.out.println(assign); assign =40 printed
} side note :
for 1st time
public static void process(Final a) process(){
{ b.a=b.a(30)+5
a.a = a.a + 5; }
} for 2nd time
} process(){
b.a=b.a(35)+5
}
30
A
35

35
B
35

35
C
40

D Compilation Error

E Runtime Error
9.
What will be the output of the following program?
public class Final
{
int printA = 20;

public static void main(String[] args)


{
PrintB c = new PrintB(); new c obj of PrintB can't be created due to compilation error

System.out.println("printA = " + c.printA); not executed


System.out.println("printB = " + c.printB); not executed
}
}

final class PrintA


{
int printA = 5;
}

final class PrintB extends PrintA Compilation error because final class PrintA can't be inherited by PrintB
{
int printA = 10;
int printB = 5;
}

printA = 5
A
printB = 5

printA = 10
B
printB = 5

printA = 20
C
printB = 5

D Compilation Error

E Runtime Error
10.
What will be the output of the following program?
public class Final
{
int lanif = 37;
int nafi = 21;

public static void main(String[] args)


{
final Final f = new Final(); new final f obj created
f.process2(); process2 called....programme jumed to proceess 2

f = modify(f); forcefully assigned to final obj f.....so compilation error....rest of the code not executed
f.process();
}

public static final Final modify(final Final f)


{
f.process();

Final f2 = new Final();


f2.process();

return f2;
}

final void process()


{
lanif = nafi + nafi;
System.out.print(lanif + " " + nafi + " ");
}

void process2()
{
nafi = lanif % 2; nafi=37%2=1
System.out.print(nafi + " " + lanif + " "); print 1 37 .....this will not be shonw in consol....it
} will be stored in cache .....due to compilation error
} it will be then discarded

A 1 37 2 1 42 21 42 21

B 18 37 36 18 42 21 42 21

C 1 37 2 1 1 37 2 1

D Compilation Error
11.
What will be the output of the following program?
public class Final
{
final int assign = 30;

public static void main(String[] args)


{
final int result = 20;
final int assign;

Final f = new Final();


assign = 20;

System.out.println(assign);
System.out.println(process(result));
}

int process(int a)
{
return a + 5;
}
}

20
A
25

B Compilation Error because assign variable is declared twice

C Compilation Error for some other reason

D Runtime Error
12.
What will be the output of the following program?
public class Final
{
final int assign = 35;

Final()
{
final int assign = 10;
print(assign); print 10
}

public static void main(String[] args)


{
final int result = 20;
final int assign;

Final f = new Final(); new obj f created


assign = 30;

f.print(result); print function called....and result is passed as a parameter .....20 printed


System.out.println(process(result) + ", " + assign + ", " + f.assign);
}
result is passed to process funtion ...in the function 5 is added
static int process(int a) and returned.....25 printed.....local variable assign is printed which
{ value was 30....global variable assign equal 35 is printed
return a + 5;
}

void print(int assign)


{
System.out.print(assign + ", ");
}
}
A 10, 20, 25, 30, 10

B 10, 20, 25, 30, 35

C 30, 20, 25, 30, 35

D Compilation Error because variable assign is declared multiple times

E Compilation Error for some other reason


13.
What will be the output of the following program?
public class Final
{
public static void main(String[] args)
{
final String result = "MERIT"; final variable result is declared and value "Merit" is assigned
final String assign; final variable assign is declared

Final f = new Final(); new f obj created result,assign and xcange is sent to
assign = "CAMPUS"; process function as parameter....then
value "CAMpUS" is assigned the programe jumps to execute process
final String xchange = " "; function

thereturned result is printed.....campus


System.out.println(process(result, assign, xchange));
merit printed
}

static String process(String a, String b, String xchange)


{
xchange = b + " " + a; campus + "" + Merit

return xchange; campus merit retured


}
}

A MERIT CAMPUS

B CAMPUS MERIT

C Compilation Error because the final variable xchange is initialised twice

D Compilation Error for some other reason

E Runtime Error
14.
What will be the output of the following program?
public class Final
{
final static short x = 2;
public static int y = 0;

public static void main(String [] args)


{
for (int z = 0; z < 3; z++) loop should run from 0 to 2....but due to compilation error loop terminates
at z =0
switch (z)
{
case y: System.out.print("0 "); // LINE M programme terminated at line M due to
compilation error.....reason is y is not a
case x - 1: System.out.print("1 "); // LINE N constant value.....y was declared as a
public varible....so it can be changed at
case x: System.out.print("2 "); // LINE O any anytime in the programme......y must
} be final for the programme to successfully
} run
}

A 012

B 012122

C Compilation fails at LINE M

D Compilation fails at LINE N

E Compilation Error at some other place


15.
What will be the output of the following program?
public class Final
{
final static short x = 2;
final static int y = 0;

public static void main(String [] args)


{
for z =0.....case y,x-1,x printed
for (int z = 0; z <= 3; z++) loop run from 0 to 3
for z=1 case x-1,x printed
for z=2 case x printed
switch (z)
for z=3 nothing printed
{
as there is no break
case y: System.out.print("0 "); // LINE M
statement.....when a case is
selected the case will be printed
case x - 1: System.out.print("1 "); // LINE N
along with rest of the remaining
cases
case x: System.out.print("2 "); // LINE O
}
} for z = 0
}
case y(0) printed.....there is no break statement.....so x-1,x are
also printed

for z = 1

case x-1(2-1=1) printed.....there is no break statement.....so x is


also printed
for z = 2

case x(2) printed

for z = 3
A 012 no case is matched....nothing printed

B 012122

C Compilation fails at LINE M

D Compilation fails at LINE N

E Compilation Error at some other place


16.
What will be the output of the following program?
class Result
{
public static void main(String args[])
{
B b = new B(); new obj b created
System.out.println("x = " + b.getResult(0, 1)); compilation error
}
}

class A
{
final public int getResult(int a, int b) { return a * b; }
}

class B extends A
{
public int getResult(int a, int b) { return a + b; } getResult(int,int) in B cannot override getResult
} (int,int) in A
here in class B getResult is trying to override
it's parent class A method getResult.....It can't
A x=1 be possible because...getResult is a final
method in class A
B x=0

C Compilation Error

D Runtime Error
17.
What will be the output of the following program?
class Result
{
public static void main(String args[])
{
A b = new B(); new ob b of Class B is created
System.out.println("x = " + b.getResult(0, 1)); 0,1 is passed to the getResult of class B
}
}
class B extends A
{
final public int getResult(int a, int b) { return a + b; } 0+1=1....1 is retuned and then printed
}

class A extends Result


{
public int getResult(int a, int b) { return a * b; }
}

A x=0

B x=1

C Compilation Error

D Runtime Error
18.
Which of the following is correct?

X: The main() method can be declared final, in addition to being public static.
Y: A class that is declared final can be subclassed.
Yes, we can declare the main () method as final in Java. The compiler does not throw
any error.

A X only If we declare any method as final by placing the final keyword then that method becomes
the final method.
The main use of the final method in Java is they are not overridden.
B Y only
We can not override final methods in subclasses.
If we are using inheritance and we need some methods not to overridden in subclasses
C X and Y then we need to make it final so that those methods can't be overridden by subclasses.
We can access final methods in the subclass but we can not override final methods.
D Both are incorrect The main purpose of using a class being declared as final is to prevent the class from
being subclassed. If a class is marked as final then no class can inherit any feature from
19. the final class.

A class that cannot be a sub-classed should be a _______.

A abstract class

B parent class

C final class

D static class

E None of these
20.
What will be the output of the following program?
final class Base {
public void testMethod() {
System.out.println("Final class");
}
}
final class Inter {
public void testMethod() {
System.out.println("Inter Class");
}
}
public class Sub extends Base { compilation error because base is a final class and Class sub can't inherit class
public void testMethod() { Base
System.out.println("Sub class extends the Base class");
}
public static void main(String arg[]) { Rest of the programme not executed
Sub sub = new Sub();
Base obj = new Base();
obj.testMethod();
Inter inte = new Inter();
inte.testMethod();
sub.testMethod();
}
}

Sub class extends the Final Base class


A Inter Class
Sub class extends the Final Base class

Final class
B Inter Class
Sub class extends the Final Base class

Final class
C Inter Class
Final class

D Some other output

E Compilation Error or Runtime Error


21.
What will be the output of the following program?
class Point {
final int x, y;
final String name;
Point(int X, int Y) {
x = X;
y = Y;
name = makeN();
}
String makeN() {
return "[" + x + "," + y + "]";
}
public String toString() {
return name;
}
}
public class Point2 extends Point {
final String c;
Point2(int x, int y, String C) {
super(x, y);
c = C;
}
String makeN() {
return super.makeN() + ":" + c;
}
public static void main(String... a) {
System.out.println(new Point2(1, 3, "Blue"));
}
}

A [1,3]:Blue

B [2,4]:Blue

C [3,1]:Blue

D Some other output

E Compilation Error or Runtime Error


22.
What will be the output of the following program?
class Base {
public String name = "Base";
}
class Derived extends Base { subclass of Base
private final String name = "Derived";
public String getName() {
return name;
}
}
public class Details { name is returned and printed using getname method
public static void main(String[] args) { from Class drived
System.out.println(new Derived().getName());
System.out.println(new Derived().name); compilation error because name has private access in
} Derived Class
}
Derived
A Base
Derived

Derived
B
Derived

Base
Derived
C
Base
Derived

D Derived

E Some other output

F Compilation Error or Runtime Error


23.
What will be the output of the following program?
public class Loopy {
public static void main(String[] args) {
final int start = Integer.MAX_VALUE - 100;
final int end = Integer.MAX_VALUE;
int count = 0;
for (int i = start; i <= end; i++)
count++;
System.out.println("Output=" + count);
}
}

A Output=99

B Output=100

C Output=101

D Some other output

E Compilation Error or Runtime Error


24.
What will be the output of following program?
public class ImportantQuestion {
public static void main(String[] args) {
Mc mc = new Mc("JAVA"); new mc obj created
System.out.println(Mc.language()); calling language method from Mc class
}
}
final class Mc {
final String java;
public Mc(String java) {
this.java = java;
}
public String language() { Here language is a non sttatic method....but we are calling the method from a
System.out.println(java); static method....which can't be possible.So compilation error
return java;
}
}

A JAVA

JAVA
B
java

JAVA
C
JAVA

D Some other output

E Compilation Error or Runtime Error


25.
What will be the output of the following program?
public final class FinalPersonClass {
private final String name; The private access modifier is accessible only within the same class.
private final int age; name,age has private access
public FinalPersonClass(final String name, final int age) {
super(); calling parent class constractor with no parameter.
this.name = name; assigning private name and age to local name and age
this.age = age;
}

public int getAge() { return age; }

public String getName() { return name; }

public static void main(String[] args) {


FinalPersonClass fpc = new FinalPersonClass("Oleti", 24); new fpc obj created
FinalPersonClass fc = new FinalPersonClass("Kiran", 25); new fc obj created
System.out.println(fpc.name + " - " + fpc.age); trying to access name within the class .....so
error will not be thrown
for fpc obj oteli,24 is sent as parameter......so
oteli,24 printed
for fc obj kiran,25 is sent as
System.out.println(fc.getName() + " - " + fc.getAge());
parameter......so kiran,25 printed
}
}

Kiran - 25
A
Kiran - 25

Oleti - 24
B
Oleti - 24

Oleti - 24
C
Kiran - 25

D Some other output

E Compilation Error or Runtime Error


25.
What will be the output of the following program?
public class FoodItems {
public static void main(String s[]) {
Noodles eat = new Noodles(); new eat obj of noodles class created
eat.print(); calling print method under noodles class....progrrame jumps to the method and prints 1~2~4
Maggi mag = new Maggi(); new mag obj of Maggi class created
mag.print(); calling print method under maggi class....progrrame jumps to the method and prints ~3
System.out.println("~" + mag.sticks); calling sticks variable under maggi class....progrrame gets the
} variable and prints ~4
}
class Maggi {
private final int spoon = 3; can't be accessed from outside of the class
protected int sticks = 4; The protected access modifier is accessible within the package. However, it can
void print() { also accessible outside the package but through inheritance only
System.out.print("~" + spoon); ~3
}
}
class Noodles extends Maggi {
private final int powder = 1;
protected int fire = 2;
void print() {
System.out.print(powder + "~" + fire + "~" + sticks); 1~2~3
}
}

A 1~2~4~3~4

B 1~2~4~1~2~3~4

C Compilation Error
D Runtime Error
26.
What will be the output of the following program?
class W
{
static int c = 0;
c is 0 when called so new W() is returned and c is incrimented
public static void main(String[] args)
by 1.....private constructor w() is automatically run....so c=1
{
printed
W w1 = c(); caling static W c()
c is 1 when called so new W() is returned and c is incrimented by
W w2 = c(w1); caling static W c(W w)
1.....private constructor w() is automatically run....so c=2 printed
W w3 = c(w2); caling static W c(W w) c is greater than ....null is returned nothing printed
W w4 = c(w3); caling static W c(W w) c is greater than ....null is returned nothing printed
}

private W()
{
System.out.println("c = " + c);
}

static W c()
{
return c++ <= 0 ? new W() : null; if c++ <=0 is true then return new w() else return null
}

static W c(W w)
{
return w.c++ == 1 ? new W() : null; if w.c++ <=0 is true then return new w() else return null
}
}

c=1
A
c=2

c=1
B c=2
c=3

c=1
c=2
C
c=3
c=4

D Compilation Error

E NullPointerException
27.
What will be the output of the following program?
class B { B.i+=

static int i;
static int j;
static {
i = 15;
j = i - 5;
}

public static void main(String[] args) {


int i = 0;
A a = null;
for (; i < 3; i++) {
a = new A();
a.i = B.i;
B.i += a.add(a.operate(i));
}
System.out.println(B.i + " " + B.j + " " + i + " " + a.i);
}
}

class A {

int i = 0;

int operate(int i) {
if (B.j - i == i * i * i) return -i;
return i * i;
}

int add(int i) {
return this.i + i;
}
}

A 14 10 3 0

B 14 10 3 16

C 120 10 3 61

D Some other output not shown here

E Compilation Error
28.
What will be the output of the following program?
class Increment
{
public static void main(String[] args)
{
Simple s = new Simple();
Simple r = new Simple();

s.incr(); calling incr method....this will incriment a by 1....so a=6 now


r.incr();
calling incr method....this will incriment a by 1.....as a is static variable a will retain its previous value
even though a new instance r is created.....so the operation will be a(6)++....so a=7 now
s.display(); print a=7 calling display method of simple class
r.display(); print a=7
}
}
class Simple
{
static int a = 5; static variable.....so variable a will be allocated memory only once.static variable will get the
void incr() memory only once, if any object changes the value of the static variable, it will retain its value.
{
a++;
}

void display()
{
System.out.println("a = " + a);
}
}

a=5
A
a=5

a=6
B
a=6

a=7
C
a=7

D Compilation Error

E Runtime Error
29.
Which of the following is incorrect?

X: super keyword and this keyword can not be used in a static method.
Y: static variables are shared by all objects and initialized when the class is first loaded.
A static method or, block belongs to the class and these will be loaded into the memory along with
the class. You can invoke static methods without creating an object. (using the class name as
A X only reference).Where the "super" keyword in Java is used as a reference to the object of the
superclass. This implies that to use "super" the method should be invoked by an object, which static
methods are not.Therefore, you cannot use the "super" keyword from a static method.
B Y only
https://www.guru99.com/java-static-variable-methods.html

The order of initialization is:

Static initialization blocks


Instance initialization blocks
Constructors
It is a variable which belongs to the class and not to object(instance)
Static variables are initialized only once , at the start of the execution.
These variables will be initialized first, before the initialization of any
instance variables
A single copy to be shared by all instances of the class
C Both are correct A static variable can be accessed directly by the class name and doesn
’t need any object
D Both are incorrect
30.
What will be the output of the following program?
public class Cricket {
static boolean ball;
public static void main(String[] args) {
short bat = 42;
if (bat < 50 & !ball) 42<50 & !0
bat++; bat =43
if (bat > 50)
;
else if (bat > 40) { 43>40
bat += 7; 43+=7 so bat =50
bat++; 50++ so bat =51
}
else
--bat;
System.out.println(bat); 51
}
}

A 41

B 42

C 50

D 51

E Compilation Error or Runtime Error


31.
What will be the output of the following program?
public class GuessCondition
{
static int a = 40; variable is static....so it will retain its value
public static void main(String args[])
{
System.out.print(a + " "); print 40
add(); a=40+40 calling add function
System.out.print(a); print 80
}
private static void add()
{
a = a + 40;
}
}
A 40 40

B 00

C 0 40

D 40 80

E Compilation Error or Runtime Error


32.
What will be the output of the following program?
public class Student {
int rollno;
String name;
static String college = "RITA"; static variable declared
static void chage() {
college = "SRIT"; AS college is static ....it will retain its value if any obj change its value
}
Student(int r, String n) {
rollno = r; initializing rollno,name
name = n;
}
void display() {
System.out.println(rollno + " " + name + " " + college);
}
public static void main(String arts[]) {
Student.chage(); chage method from student is called.....collage variable value changed to SRIT
Student s1 = new Student(516, "Kiran"); new s1 obj created with parameter
Student s2 = new Student(560, "Vishwanath"); new s2 obj created with parameter
Student s3 = new Student(517, "Kranthi"); new s3 obj created with parameter
s1.display(); 516 Kiran SRIT display method of class student is called using s1 obj ....where parameter
s3.display(); 517 Kranthi SRIT are 516, "Kiran"
s2.display(); 560 Vishwanath SRIT similarly s3 and s2
}
}

516 Kiran SRIT


A 516 Kiran SRIT
516 Kiran SRIT

516 Kiran SRIT


B 517 Kranthi SRIT
560 Vishwanath SRIT
516 Kiran RITA
C 516 Kiran RITA
516 Kiran RITA

516 Kiran RITA


D 517 Kranthi RITA
560 Vishwanath RITA

E Compilation Error or Runtime Error


33. Static variables are the shared variables. So you can access
them using either the Classname.staticVariable or using an object
What will be the output of the program? of the class instance.staticVariable. In any case you will be
public class Karbon { referring to the single copy of the variable in memory, no matter
static int i = 5; static variable how many objects you create.
public static void main(String[] args) {
Karbon test = null;
System.out.print("i = " + test.i + ", "); accessing i static way....print 5
System.out.print("i = " + Karbon.i); accessing i static way....print 5
}
}

A i = 0, i = 5

B i = 5, i = 5

C Compilation Error

D NullPointerException

E Runtime Error for some other reason


34.
What will be the output of the following program?
public class CanYouDoIt {
static boolean bool;
static int[] iary = new int[1]; int array with 1 element
static char chr;
static boolean[] barr = new boolean[1]; boolean array with 1 element
public static void main(String args[]) {
boolean b = false;
if (bool) { false so if condition not executed executing else statement
b = (chr == iary[chr]);
} else {
b = (barr[chr] = bool); b= false and barr[chr]=false assigned
System.out.println(b + " " + barr[chr]);
}
}
}

A false false

B true true

C false true

D true false

E Compilation Error or Runtime Error


35.
Why is the Java main method static?
What will be the output of the following program? Ans) It is because the object is not required
public class Sketch { to call a static method. If it were a non-static
static int ad = 100; static variale ad and bc declared method, JVM creates an object first then call
static int bc = 200; main() method that will lead the problem of
static { Java static block extra memory allocation.
ad += 1; Is used to initialize the static data member.
bc += 1; It is executed before the main method at the time of classloading.
}
before main method is run....value of ad =201 and bc =
public static void main(String args[]) {
401 because all static block is run by the programme
ad += 5; 201+5=206
before main method...so for ad it will be 100+1+100=201
bc += 10; 401+10=411 and for bc it will be 200+1+200=401
System.out.println(ad + bc); 411+206=617
}
static {
ad += 100;
bc += 200;
}
}

A 317

B 615

C 617

D 315

E Compilation Error or Runtime Error


36.
What will be the output of the following program?
public class KeepItClean {
public static void main(String[] args) {
Valuable.status(); s5
try(Valuable v = new Valuable(); Valuable v2 = new Valuable()) 04 03
{
Valuable.status(); S3
v.open(); 02
Valuable.status(); S2
v2.open(); 01
Valuable.status(); S1 executing of try block is finished....so closing obj v and v2....calling close
} method of Valuable for each obj.....print c2 c3
Valuable.status(); s3
} Autoclosable explain : https://www.youtube.com/watch?v=
} iVdTCqhTUFA
class Valuable implements AutoCloseable {
static int numberOfValuables = 5; https://docs.oracle.com/javase/8/docs/api/java/lang/AutoCloseable.h
Valuable() { tml
open(); everytime new obj is created this constractor is autometically run
}
public void open() {
numberOfValuables--;
System.out.print("O" + numberOfValuables + " ");
}
public void close() {
numberOfValuables++;
System.out.print("C" + numberOfValuables + " ");
}
static void status() {
System.out.print("S" + numberOfValuables + " ");
}
}

A S5 S5 O4 S4 O3 S3 S3

B S5 S5 O4 S4 O3 S3 C4 c5 S5

C S5 O4 O3 S3 O2 S2 O1 S1 S1

D S5 O4 O3 S3 O2 S2 O1 S1 C2 C3 S3

E Some other output

F Compilation Error or Runtime Error


37.
What will be the output of the following program
public class Time {
int a = 50; compilation error because a anb variable are not static.....but we are doing arethmatic operation of not
int b = 10; static variable inside of static main method....which is not possible
public static void main(String args[]) {
a += b--;
System.out.println(a);
}
}
A 60

B 50

C 59

D 49

E Compilation Error or Runtime Error


38.
What will be the output of the following program?
class GoodBoy {
int a;
private final int b;
static int c = 7;
GoodBoy() {
a = 77;
b = 55;
c = 33;
}
public void getBoy() {
System.out.print("-GoodBoy-");
}
}
class GoodGirl extends GoodBoy {
int a;
private final int b;
static int c = 40;
GoodGirl() {
a = 33;
b = 22;
c = 11;
}
public void getBoy() {
System.out.print("-GoodGirl-");
}
}
public class SolveIt {
public static void main(String arg[]) {
GoodBoy a = new GoodBoy();
GoodBoy b = new GoodGirl();
a.getBoy(); getboy from GoodBoy class is called....-GoodBoy- is printed
b.getBoy(); getboy from GoodGirl class is called....-GoodGirl- is printed
System.out.print("-" + GoodBoy.c + "-"); 33 printed....because when obj of Goodboy class is
System.out.print("-" + GoodBoy.c + "-"); created....GoodBoy constructor is autometically run ....inside
} the constractor...c=33 is assigned....as c is static value it
} will retain its value.....so c =33 printed

A -GoodBoy--GoodGirl--7--7-
B -GoodBoy--GoodGirl--33--11-

C -GoodBoy--GoodGirl--33--33-

D -GoodBoy--GoodGirl--40--40-

E Some other output

F Compilation Error or Runtime Error


39.
What will be the output of the following program?
That (byte...x) feature is called varargs. It
public class CallBy { means that function can receive multiple String
static void execute(Byte x, Byte y) { arguments.The varargs short for variable-
System.out.println("execute, execute"); length arguments is a feature that allows the
} method to accept variable number of
static void execute(Byte x, Byte y, Byte z) { arguments (zero or more). With varargs it
System.out.println("execute, execute, execute"); has become simple to create methods that
} need to take a variable number of arguments.
static void execute(byte... x) {
System.out.println("execute");
}
public static void main(String[] args) {
byte b = 5;
execute(b); calling static void execute(byte... x) as only 1 parameter is sent
execute(b, b); calling static void execute(Byte x, Byte y)
execute(b, b, b); calling static void execute(Byte x, Byte y, Byte z)
execute(b, b, b, b); calling static void execute(byte... x) as multiple argument parameter is sent
}
}

execute
execute, execute
A
execute, execute, execute
execute, execute, execute, execute

execute
execute, execute
B
execute, execute, execute
execute

execute
C
execute
execute
execute

D Some other output

E Compilation Error or Runtime Error


40.
What will be the output of the following program?
public class ForForever {
static int i = 0;

public static void main(String[] args) {


for (int i = 0; check(i); i++) {
System.out.print(i + "~" + ForForever.i + ","); local i +~+static i + ","
}
}

private static boolean check(int j) {


i = j; static i = local j
return i < 3; static i <3
}
}

A 0~0,1~0,2~0,

B 0~0,1~1,2~2,

C Some other output

D Compilation Error

E Goes into infinite loop


41.
What is the output of the following program?
public class AutoFare {

private static final double MINIMUM_CHARGE = 16.0;


protected static final double PRICE_PER_KM = 9.25;
public static final double WAITING_CHARGE_PER_MIN = 0.50;

public static void main(String[] args) {


System.out.print(calculate(0.5, 12) + "@");
System.out.print(calculate(12, 0) + "@");
System.out.print(calculate(1, 200) + "@");
System.out.print(calculate(8.25, 3) + "@");
}
private static int calculate(double kms, int minutes) {
return Math.rint(Math.min(kms * PRICE_PER_KM + WAITING_CHARGE_PER_MIN,
MINIMUM_CHARGE)); compilation error because calculate method is int type.....but the operation inside the calculate
} method returns double type result.....so it cause possible lossy conversion from double to int
} math.rint is used to round the argument to nearest mathematical integer.
The static function Math.min() returns the lowest-valued number passed into it,

A 5@16@10@16@

B 5@16@9@16@

C 16@111@16@76@

D Compilation Error or Runtime Error


42.
What will be the output of the following program?
public class FireAlarm {
public static void main(String[] args) {
double d[] = {2.3,3.4,4.5};
p(d); print 2.3,3.4,4.5
modify(d); p(d); print 3.4,4.5,3.4 jump to modify aND EXECUTE THE METHOD...jump to p method
modify2(d); p(d); print 3.4,4.5,3.4 and execute it
modify(d); p(d); print 4.5,3.4,4.5
}
private static void p(double[] d) {
System.out.print("{");
for (double d1 : d) { acces d arrey with d1
System.out.print(d1 + ","); print 2.3,3.4,4.5
}
System.out.print("},");
}
private static void modify(double[] d) {
d[0] = d[1]; d[0]=3.4 for second time
d[1] = d[2]; d[1]=4.5 d[0]=4.5
d[2] = d[0]; d[2]=3.4 d[1]=3.4
} d[2]=4.5
private static void modify2(double[] d) {
d = new double[3];
d[0] = d[1] = d[2] = 1.0; here d is new array....not the previous array...so the value of previous array
} will not be changed
}

A {2.3,3.4,4.5,},{3.4,4.5,3.4,},{3.4,4.5,3.4,},{4.5,3.4,4.5,},

B {2.3,3.4,4.5,},{3.4,4.5,3.4,},{1.0,1.0,1.0,},{1.0,1.0,1.0,},

C {2.3,3.4,4.5,},{3.4,4.5,2.3,},{3.4,4.5,2.3,},{4.5,2.3,3.4,},

D Some other output or Compilation Error


43.
What will be the output of the following program?
public class OrangeMangoBanana {
final static int orange, mango;
static int banana;
static {
mango = orange + 3; throws compilation error....because final variable orange is not initialized and orange is
banana = mango * 2; blank....here the programme tries to do a arethmatic operation with blank variable
} if the indicated static block is written before the arethmatic
public static void main(String[] args) { operation then the proggramme will successfully execute
System.out.print(orange + " " + mango + " " + banana + " ");
banana = mango + orange + banana;
System.out.print(banana);
}
static {
orange = 25;
}
}

A 25 28 56 109

B 0369

C Some other output

D Compilation Error
44.
What will be the output of the following program?
public class NValueIs {
public static void main(String[] args) {
int num1 = 1;
int num2 = 2;
System.out.println("Before swap method, num1 is " + num1 + " and num2
is " + num2);
swap(num1, num2);
System.out.println("After swap method, num1 is " + num1 + " and num2
is " + num2);
}
public static void swap(int n1, int n2) {
int temp = n1;
n1 = n2;
n2 = temp;
}
}

Before swap method, num1 is 1 and num2 is 2


A
After swap method, num1 is 2 and num2 is 1
Before swap method, num1 is 1 and num2 is 2
B
After swap method, num1 is 1 and num2 is 2

C Some other output

D Compilation Error or Runtime Error


45.
What will be the output of the following program?
public class Boolean {
private boolean myFlag1 = true;
private static final boolean myFlag2; variable myFlag2 not initialized in the default constructor
static final double weeks = 9.5;
protected static final int BOXWIDTH = 42;
public static void main(String[] arguments) {
Boolean m = new Boolean();
System.out.println(m.myFlag1);
System.out.println(myFlag2);
System.out.println(weeks);
System.out.println(BOXWIDTH);
}
}

true
false
A
9.5
42

false
false
B
9.5
42

true
ture
C
9.5
42

D Some other output

E Compilation Error or Runtime output


46.
What will be the output of the following program?
public class Programming {
static boolean b;
static int[] ary = new int[1]; int array with 1 element
static char chr;
static boolean[] boolarr = new boolean[1]; bool array with 1 element
public static void main(String args[]) {
boolean a = false;
if (b) { if condition not executed....because b is false...else statement executed
a = (chr == ary[chr]);
} else {
a = (boolarr[chr] = b); a and boolarr[chr] assigned false
System.out.println(a + " " + boolarr[chr]); fslse false printed
}
}
}

A true true

B false false

C true false

D false true

E Compilation Error or Runtime Error


47.
What will be the output of the following program?
public class PowerExample {
static int i, j, k;
final int result1 = 0;
final double result = 1;
public static void main(String[] args) {
i = 1;
j = 2;
k = 3;
int val = getResult(i, j, k);
System.out.println("final result is " + val);
}

private static int getResult(int i, int j, int k) {


int result = getDiv(i, j, k);
return result;
}
private static int getDiv(int i, int j, int k) { here result is local
int result = (getpow(i, 2, result) + getpow(j, 3, result)) / k; variable and it is not
return result; initialized....so
} compilation error
private static int getpow(int i, int j, double result) {
result = 1;
for (int k = 0; k < j; k++) {
result *= i;
}
return (int) result;
}
}

A final result is 3

B final result is 2

C final result is 0

D Some other output

E Compilation Error or Runtime Error


48.
What will be the output of the following program?
class Desktop extends TSystem {
public String getString() {
Laptop l = new Laptop(); new l obj of laptop class created
str += l.getString(); getstring of laptop class called which returns "Welcome"
return str + "Hello"; return WelcomeHello
}
}

class Laptop extends TSystem {


public String getString() {
return "Welcome";
}
}

public class TSystem {


static String str = "";
public String getString() {
Desktop d = new Desktop(); new d obj of desktop class created
str += d.getString(); getstring of desktop class called which returns "WelcomeHello"
return str + "Hai"; retrn WelcomHelloHai
}
public static void main(String[] args) {
TSystem ts = new TSystem();
str += ts.getString();
System.out.println(str); print WelcomHelloHai
}
}

A HaiHelloWelcome

B WelcomeHelloHai
C HelloHaiWelcome

D HaiWelcomeHello

E Some other output

F Compilation Error or Runtime Error


49.
What will be the output of the following program?
public class ComplexType {
private final int a;
int b, total;
protected static int c; Calculation milate parini
ComplexType(int a, int b, int c, int total) {
this.a = a;
this.b = b;
ComplexType.c = c;
this.total = total;
}
ComplexType(int a, int b, int c) {
this(a, b, c, a + c + 3);
}
ComplexType(int a, int b) {
this(a, b, c, a + b + c);
}
public void print() {
System.out.println("c = " + c + " total = " + total);
c = total;
}
public static void main(String[] args) {
ComplexType t1 = new ComplexType(4, 7, 2, 4 + 7 + 2);
t1.print();
ComplexType t3 = new ComplexType(ComplexType.c, ComplexType.c);
ComplexType t2 = new ComplexType(t1.a, ComplexType.c, t1.b);
t3.print();
t2.print();
}
}

c = 2 total = 13
A c = 13 total = 6
c = 7 total = 14

c = 2 total = 13
B c = 7 total = 6
c = 6 total = 14
c = 2 total = 13
C c = 7 total = 39
c = 39 total = 14

c = 7 total = 6
D c = 6 total = 13
c = 13 total = 14

E Some other output

F Compilation Error or Runtime Error


50.
What will be the output of the following program?
public class LongDivision {
private static final long VALUE1 = 24 * 60 * 60 * 1000; 86400000
private static final long VALUE2 = 24 * 60 * 60 * 1000 * 1000; 500654080
public static void main(String[] args) {
System.out.print(VALUE2 / VALUE1); 500654080/86400000
}
}

A 5

B 1000

C 5000

D Some other output

E Compilation Error or Runtime Error


51.
What will be the output of the following program?
public class Vinayak {
static int laddu = 4;
final static String TRUTH = "GaneSh iS soO great";
static String trunk = "";
static float mouse = 12.0f;
public static void main(String[] args) {
ganesh(); laddu =4
ganesh(); laddu =1
ganesh(); laddu =1
ganesh(); laddu =1
ganesh(); laddu =1
ganesh(); laddu =1
System.out.println(trunk);
}
public static void ganesh() {
for 1st time laddu=4 for second to 6th time laways laddu =1
vignesh(laddu);
switch (laddu) { for 1st time switch(4) so default case.....laddu value changed to 1
case 5 * 3 : for second to 6th time always laddu =1 so default case.....laddu value changed to 1
case 18 :
laddu -= 3;
break;
case 10 :
laddu = 2;
break;
default :
laddu = 1;
break;
case 2 :
laddu = 0;
}
}
public static void vignesh(int i) {
trunk += i; for first time trunk+=4 for second to 6th time always trunk+=1
}
} so 411111

A 411111

B 422222

C Compilation Error

D Runtime Error
52.
What will be the output of the following program?
class Draft {
private static String[] String;
static {
main(String);
System.out.print("%");
} jump
public static void main(String[] args) {
main(new String());
System.out.print("#"); print # go back
}
static void main(String args) {
System.out.print("@"); jump...print @ go back
}
static {
jump
System.out.print("&");
}
static { jumo ,....print @ #
main(String);
System.out.print("~"); print ~
}
then jump to main method print @ #

static {
System.out.print("$"); print $
}
}

A %&@#~$@#

B @%&@#~$#

C %&-$@#@#

D @#%&@#~$@#

E Compilation Error or Runtime Error


53.
What will be the output of the following program?
class SamsungMobile
{
public static void main(String[] args)
{
Simple.getData(); call static method getdata....print a=80
Simple.display(); call static method display....print a=20
}
}
class Simple
{
static int a, b = 20;
static void getData()
{
System.out.println("a = " + a);
}
static void display()
{
System.out.println("b = " + b);
}
static as it is a static java block it will execute first before execution of the getdata method
{
System.out.println("Samsung Mobile");
a = 4 * b; 4*20=80
}
}

Samsung Mobile
A a=0
b = 20
Samsung Mobile
B a = 80
b = 20

a=0
C
b = 20

D Compilation Error

E Runtime Error
54.
What will be the output of the following program?
class Jumbo
{
static
{
i = 5;
}
static int i;

public static void main(String[] args)


{
System.out.println("i value is " + i); static block initializes i =5
}
}

A i value is 0

B i value is 5

C i value is (some reference value)

D Compilation Error
55.
Which of the following false java statement?

A A class cannot be private.

B A cannot be static.

C A class cannot be protected.

D A class cannot be final.


E None of the above
56.
What will be the output of the following program?
class Mobile {
static String str = "";
protected Mobile() {
str += "Mobile is : ";
}
}
class Airtel extends Mobile {
private Airtel() {
str += "Airtel";
}
} a obj of docomo is called.docomo class inherits all poperties of mobile
public class Docomo extends Mobile { class....so docomo class has both mobile and docomo constractor
private Docomo() { ....both of them are run autometically.
str += "Docomo";
}
public static void main(String args[]) {
new Docomo();
System.out.println(str);
}
}

A Docomo

B Mobile is : Airtel

C Mobile is : Docomo

D Some other output

E Compilation Error or Runtime Error


57.
What is the output of following program?
public class RiversOverflow {

public static void main(String[] args) {


int a = 0;
int b = 9;
RiversOverflow ro = null;
int c = ro.ganga(a, b); ro.ganga(0,9)....return 9
long d = ro.yamuna(a, b); ro.yamuna(0,9)...return 9
ro = new RiversOverflow(); new obj created
long e = ro.ganga(c, d); ro.ganga(9,9) returns 18
System.out.print("a" + b + "c" + d + "e"); prints a9c9e
}

private static int ganga(int a, int b) {


System.out.print("a" + b); a9
return a + b; 0+9=9
}

private static int yamuna(int b, int a) {


System.out.print(a + "b"); 9b
return a - b; 9-0=9
}

private long ganga(int a, long b) {


System.out.print(a + b); print 18....-->9+9=18
return a + b; return 18
}
}

A 9909

B a99b18a9c9e

C a90b0a9c-9e

D Some other output

E Compilation Error or Runtime Error


58.
What will be the output of the following program?
public class ClassTest {
public static void main(String[] args) {
System.out.println("Class ClassTest"); prints Class ClassTest
}
public static class A {
A() {
System.out.println("Class A");
} not executed because these were not called from
} main method
public static class B {
B() {
System.out.println("Class B");
}
}
}

Class ClassTest
A Class A
Class B

Class ClassTest
B
Class A
Class ClassTest
C
Class B

Class A
D
Class B

E Some other output

F Compilation Error or Runtime Error


59.
What will be the output of the following program?
public class Null {
public static boolean greet() {
System.out.print("Program execute = "); prints program execute =
return true;
}
public static void main(String[] args) {
System.out.println(Null.greet()); prints true
}
}

A Program execute = true

B Program execute = false

C Program execute = null

D Program execute =

E Some other output

F Compilation Error or Runtime Error


60.
What will be the output of the following program?
class Draft {
private static String[] String;
static {
Draft.main(String);
System.out.print("%");
}
public static void main(String[] args) {
Draft.main(new String());
System.out.print("#");
}
static void main(String args) {
System.out.print("@");
}
static {
System.out.print("&");
}
static {
Draft.main(String);
System.out.print("~");
}
static {
System.out.print("$");
}
}

A %&@#~$@#

B @%&@#~$#

C %&-$@#@#

D @#%&@#~$@#

E Some other output

F Compilation Error or Runtime Error


61.
What will be the output of the following program?
public class LovePureImpure {
static {
System.out.println("Love is neither pure nor impure"); static method printed
}
public LovePureImpure() {
System.out.println("Love is purely impure and impurely pure");
}
public static void main(String[] args) {
Pure pure = null;
Impure impure = new Impure(); impure obj created.....it calls impure constractor and static block
pure = new Pure(); pure obj created....calls the constractor....static method not executed because it was
} already executed by impure obj.....static method is executed only once
}
class Pure {
static {
System.out.println("I am pure Love");
}
public Pure() {
System.out.println("I am impure Love disguised as pure Love");
}
}
class Impure extends Pure {
public Impure() {
System.out.println("I am impure Love");
}
}

Love is neither pure nor impure


I am pure Love
A I am impure Love disguised as pure Love
I am impure Love
I am impure Love disguised as pure Love

Love is neither pure nor impure


I am impure Love
B
I am pure Love
I am impure Love disguised as pure Love

Love is neither pure nor impure


Love is purely impure and impurely pure
C I am impure Love
I am pure Love
I am impure Love disguised as pure Love

D Some other output or Compilation Error or Runtime Error


62.
What will be the output of the following program?
public class LovePureImpure {
static {
System.out.println("Love is neither pure nor impure"); static method printed
}
public LovePureImpure() {
System.out.println("Love is purely impure and impurely pure"); onj of lovepureimpure not
} created so constractor not
public static void main(String[] args) { called
Pure pure = null;
Impure impure = new Impure(); impure obj created .....print impure constractor
pure = new Pure(); pure obj created .....print static block and pure constractor
}
}
class Pure {
static {
System.out.println("I am pure Love");
}
public Pure() {
System.out.println("I am impure Love disguised as pure Love");
}
}
class Impure {
public Impure() {
System.out.println("I am impure Love");
}
}

Love is neither pure nor impure


I am pure Love
A I am impure Love disguised as pure Love
I am impure Love
I am impure Love disguised as pure Love

Love is neither pure nor impure


I am impure Love
B
I am pure Love
I am impure Love disguised as pure Love

Love is neither pure nor impure


Love is purely impure and impurely pure
C I am impure Love
I am pure Love
I am impure Love disguised as pure Love

D Some other output or Compilation Error or Runtime Error


63.
What will be the output of the following program?
public class LovePureImpure {
static {
System.out.println("Love is neither pure nor impure"); static method printed
}
public LovePureImpure() {
System.out.println("Love is purely impure and impurely pure");
}
public static void main(String[] args) {
new LovePureImpure(); new obj of LovePureImpure created .....print LovePureImpure constractor
Pure pure = null;
Impure impure = new Impure(); impure obj created .....print impure constractor
pure = new Pure(); pure obj created .....print static block and pure constractor
}
}
class Pure {
static {
System.out.println("I am pure Love");
}
public Pure() {
System.out.println("I am impure Love disguised as pure Love");
}
}
class Impure {
public Impure() {
System.out.println("I am impure Love");
}
}

Love is neither pure nor impure


I am pure Love
A I am impure Love disguised as pure Love
I am impure Love
I am impure Love disguised as pure Love

Love is neither pure nor impure


I am impure Love
B
I am pure Love
I am impure Love disguised as pure Love

Love is neither pure nor impure


Love is purely impure and impurely pure
C I am impure Love
I am pure Love
I am impure Love disguised as pure Love

D Some other output or Compilation Error or Runtime Error


64.
public class Rough1 {
public static void main(String[] args) {
byte b = getStatusValue(255);
System.out.println(b); print 127
b = Rough2.getStatusValue(122);
System.out.println(b); print -1
}
static byte getStatusValue(Object obj) {
if (obj != null) 255!=null
return 127;
else
return -1;
}
}
class Rough2 extends Rough1 {
static byte getStatusValue(Object obj) {
if (obj != null)
System.out.println(obj); print 127
else
return -1;
return (byte) 255;
}
}

127
A 122
-1

-1
B 122
-1

C Compilation Error

D Runtime Error
65.
Which of the following is the correct way of defining the main method?

class MeritCampus
{
static public void main(String[] args)
A {
System.out.println("Merit Campus");
}
}

class MeritCampus
{
public static void main(String[] args)
B {
System.out.println("Merit Campus");
}
}

class MeritCampus
C
{
static public int main(String[] args)
{
System.out.println("Merit Campus");
return 1;
}
}

D Both options A and B


66.
Which of the following is correct?

X: If we do not provide String array as the argument to the main method as shown below,
then the program compiles but throws a run-time error "NoSuchMethodError".
class First
{
public static void main()
{
System.out.println("Hello World");
}
}

Y: If static modifier is removed from the signature of the main method as shown below,
then the program compiles but throws a run-time error "NoSuchMethodError".
class Second
{
public void main(String []args)
{
System.out.println("Hello World");
}
}

A X only

B Y only

C Both are correct

D Both are incorrect


67.
Which of the following declarations is valid for main method?

A public static String main(String args[])


B protected static void main(String args[])

C final public static void main(String args[])

D public static void main()

E private static void main(String args[])


68.
What will be the output of the following program?
class MethodCalls
{
public static void main(String[] args)
{
a1(8);
}

static int a1(int a1)


{
System.out.print(" a1 = " + a1); 8 4 2 1 0
return a2(a1++); 8++....first sent 8 to a2 then incriment 4++..first sent 4 to a2 then incriment
} 2++..first sent 2 to a2 then incriment 1++..first sent 1 to a2 then incriment
0++..first sent 0 to a2 then incriment
static int a2(int a2)
{
if (a2 == 0)
{
return 10; when a2 =0 return 10....program complete
}
System.out.print(" a2 = " + a2); 8 4 2 1
return a1(a2 / 2);
4 2 10
}
}

A a1 = 8 a2 = 8 a1 = 4 a2 = 4 a1 = 2 a2 = 2 a1 = 1 a2 = 1 a1 = 0 a2 = 0

B a1 = 8 a2 = 8 a1 = 4 a2 = 4 a1 = 2 a2 = 2 a1 = 1 a2 = 1 a1 = 0

C a1 = 1 a2 = 1 a1 = 2 a2 = 2 a1 = 4 a2 = 4 a1 = 8 a2 = 8

D Compilation Error

E Goes into infinite loop


69.
What will be the output of the following program?
class DefineClasses
{
int c;
public static void main(String[] args)
{
A a = new A(); new obj a created of class A
B b = new B(); new obj b created of class B
System.out.println("a = " + a.a); a var of A class print
System.out.println("b = " + b.b); b var of B class print
System.out.println("c = " + c); trying to access non static variable c from static main method....throws
} compilation error
}

class A { int a; }

class B { int b = 5; }

a=0
A b=5
c=0

a=0
B b=0
c=0

C Compilation Error

D Runtime Exception
70.
What will be the output of the following program?
class Interest
{
double amount, rate, interest = -1.0;
static int numberOfYears;

public static void main(String[] args)


{
Interest i = new Interest(); new obj of interest class created
System.out.println("i = " + i.interest); -1 initialized -1 in interest global variable
i.amount = 3000;
i.rate = 4.25f;
i.numberOfYears = 2;
i.interest = 0;
updateInterest(i);
System.out.println("i = " + i.interest); 255 return from update
}

private static void updateInterest(Interest i)


{
System.out.println("i = " + i.interest); 0 assigned 0 in interest global variable
i.interest = i.amount * i.rate * i.numberOfYears / 100.0; (3000*4.25*2)/100=255
}
}

i = -1.0
A i = 0.0
i = 255.0

i = -1.0
B i = 0.0
i = 0.0

i = -1.0
C i = 0.0
i = 25500.0

D Compilation Error

E Throws NullPointerException
71.
What will be the output of the following program?
class Total {
int a, b, total;
static int c;

Total(int a, int b, int c, int total) {


this.a = a; this.b = b; this.c = c; this.total = total;
}

Total(int a, int b, int c) {


this(a, b, c, a + b + c);
}

Total(int a, int b) {
this(a, b, c, a + b + c);
}

public void print() {


System.out.println("c = " + c + " total = " + total);
c = total;
}

public static void main(String[] args) {


Total t1 = new Total(4, 7, 2, 4 + 7 + 2);
Total t2 = new Total(t1.a, t1.b, t1.c);
t1.print();
Total t3 = new Total(t1.c, t2.c);
t2.print(); t3.print(); t1.print();
}
}

c = 2 total = 13
c = 13 total = 13
A
c = 13 total = 6
c = 6 total = 13

c = 2 total = 13
c = 13 total = 13
B
c = 13 total = 39
c = 39 total = 13

c = 2 total = 13
c = 2 total = 13
C
c = 0 total = 26
c = 13 total = 13

D Compilation Error

E Run-time Error
72.
What will be the output of the following program?
class OutPut
{
int D;

public static void main(String[] args)


{
PrintA a = new PrintA(); new obj of PrintA and PrintB created
PrintB c = new PrintB();

System.out.println("printA = " + a.printA);


System.out.println("printB = " + c.printB);

System.out.println("D = " + D); compilation error beacause D is a non static vaiable but we are trying
} to access D from static method
}

class PrintA
{
int printA;
}

class PrintB
{
int printB = 5;
}

printA = 0
A printB = 5
D=0

printA = 0
B printB = 5
D = (some object reference value)

C Compilation Error

D Runtime Error
73.
class OutPut
{
int c; as no value is initialized so c=0

public static void main(String[] args)


{
PrintA a = new PrintA(); new obj of PrintA and PrintB created
PrintB c = new PrintB();

System.out.println("printA = " + a.printA);


System.out.println("printB = " + c.printB);

System.out.println("c = " + new OutPut().c); printing non static variable c using obj of output
} class
}

class PrintA
{
int printA; as no value is initialized so PrintA=0
}

class PrintB
{
int printB = 5;
}

printA = 0
A printB = 5
c = (some object reference value)
printA = 0
B printB = 5
c=0

C Compilation Error

D Runtime Error
74.
What will be the output of the following program?
Oka('A')--> prints A
public class Delta ....returns true
{ Oka('B')--> prints B
static boolean Oka(char c) ....returns true
{ i<2 returns true
System.out.print(c + " "); so.....programme goes
return true; inside the loop
} i is incrimented by 1
and
public static void main(String[] argv) Oka('D')--> prints D
{ ....returns true
int i = 0; loop continues...

for (Oka('A'); Oka('B') && (i < 2); Oka('C')) Oka('C')--> prints C


{ ....returns true
i++; Oka('B')--> prints B
Oka('D'); ....returns true and i<2
} returns true because i is
} now 1 so programme
} goes inside the loop
i is incrimented by 1
and
A ABCDABCD Oka('D')--> prints D
....returns true and Oka
('C')--> prints C
B ABDCBDCB ....returns true
Oka('B')--> prints B
C ABDCBDC ....returns true and i==2
so loop exits
D Compilation Error
final result ABDCBDCB
E Runtime Error
75.
Which of the following is incorrect?

X: instance methods use dynamic (late) binding.


Y: static methods use static (early) binding.

A X only
B Y only

C Both are correct

D Both are incorrect


76.
What will be the output of the following program?
public class MyBasicStaticMemberClass
{
public static class MyStaticMemberExampleClass
{
public void printStatus()
{
System.out.println("Inside static member class");
}
}
public static void main(String a[])
{
StaticMemberTestClass staticMemberTest = new StaticMemberTestClass(); create new obj
staticMemberTest.testMemberClass(); calling testMemberClass() using obj....program jumps to
System.out.println("Inside Main"); testMemberClass() method
}
print inside main
static
{
System.out.println("Static Block"); first run static block autometically
}
}
class StaticMemberTestClass
{
public void testMemberClass()
{
MyBasicStaticMemberClass.MyStaticMemberExampleClass myStaticMember
Example = new MyBasicStaticMemberClass.MyStaticMemberExampleClass(); create new obj of MyBasic
myStaticMemberExample.printStatus(); staticMemberClass.MyStaticMemb
System.out.println("Inside Class"); erExampleClass
}
} calling isnside class
printstatus method()
print inside class and
...it prints inside static member class
return to main method
Static Block
Inside static member class
Inside static member class
A
Inside Main
Inside static member class
Inside Class

Inside Class
B
Inside Main
Static Block
Inside static member class

Static Block
Inside static member class
C
Inside Main
Inside Class

Static Block
Inside static member class
D
Inside Class
Inside Main

E Compilation Error or Runtime Error


77.
What will be the output of the following program?
public class Convert {

public void printBinaryFormat(int dec) {


int bin[] = new int[15]; new int array with 15 element
int temp = 0;
while (dec > 0) {
bin[temp++] = dec % 2; dividing the number with 2 and storing the mod value in the array until the
dec = dec / 2; number becomes eual or less than 0
}
System.out.print("After binary conversion: ");
for (int i = temp - 1; i >= 0; i--) {
System.out.print(bin[i]); printing from most to least significant bit
}
}

public static void main(String a[]) {


Convert dec = new Convert(); creat obj
dec.printBinaryFormat(8489);
}
}

A After binary conversion: 10000100101001

B After binary conversion: 10000110001100

C After binary conversion: 10000100100100

D Compilation Error or Runtime Error


78.
What will be the output of the following program?
public class HaiTwice {
static int num;
public static void main(String[] args) {
HaiTwice p = new HaiTwice(); create obj
p.start(); call start metod using obj
System.out.println(num); print 14
}
void start() {
int var = 7;
twice(var); caling twice method
System.out.print(var + " "); locsl vsrisble var =7....so print 7
}
void twice(int var) {
var = var * 2;
num = var; num is static variable.....v*2=14 ase var =7.....so =14 and it will retain value
}
}

A 77

B 7 14

C 14 0

D 14 14

E Compilation Error or Runtime Error


79.
What will be the output of the following program?
public class Stand {
public static void main(String avs[]) {
haiArray(4);
}
static int[][] haiArray(int size) {
int[][] printArray = new int[size][]; initialite matrix with row number.....here size is row number
int val = 1;
for (int i = 0; i < printArray.length; i++) { new int array with dynamic array size....size through
changes from 1 to 4
printArray[i] = new int[i + 1];
for (int j = 0; j < printArray[i].length; j++) {
printArray[i][j] = val++;
System.out.print(printArray[i][j] + " ");
}
System.out.println();
}
return printArray;
}
}
printArray[i][j]=val++
1234
5678 here for 1st time i=0,j=0
A printArray[0][0]=1...then
9 10 11 12 prints it...loop exit i++
here for 2nd time,i=1,j=0
13 14 15 16 printArray[1][0]=2
printArray[1][1]=3...then
1234 prints it...loop exit i++
here for 3rd time,i=2,j=0
567 printArray[2][0]=4
B
printArray[2][1]=5
89
printArray[2][2]=6...then
10 prints it....loop exit i++
here for 4th time,i=3,j=0
printArray[3][0]=7
1 printArray[3][1]=8
23 printArray[3][2]=9
C printArray[3][3]=10...then
456 prints it....loop
exit...program terminate
7 8 9 10

D 1234

E Compilation Error or Runtime Error


80.
What will be the output of the following program?
public class Printstatment14 {
public static void Elements(int[] arr) {
for (int i = 0; i < arr.length; i++) {
boolean isValid = false;
for (int j = 0; j < i; j++) { 1st time : i=0,j=0....so loop not execute...if is true....print 5
if (arr[i] == arr[j]) { 2nd time : i=1,j=0....so loop executed...arr[1]==arr[0]==2=5...if
isValid = true; is false...lop terminate....second if true....print 2
break; 2nd time : i=2,j=0....so loop executed...arr[2]==arr[0]==7=5...if
} is false...loop continue...arr[2]==arr[1]==7=2..if is false...lop
} terminate....second if true....print 7....Rest is semilarly calculated
if (!isValid) {
System.out.print(arr[i] + " ");
}
}
}
public static void main(String a[]) {
int[] nums = {5, 2, 7, 2, 4, 7, 8, 2, 3};
Printstatment14.Elements(nums);
}
}

A 527483

B 54783
C 4782

D 27

E 2

F Compilation Error or Runtime Error


81.
What will be the output of the following program?
public class PrintStatment10
{
public static void main(String args[])
{
PrintStatment10 obj = new PrintStatment10(); new obj create
obj.printBinaryFormat(23); calling printBinaryFormat() with 23 as argument using obj
}
public void printBinaryFormat(int number)
{
int binary[] = new int[25]; create int array wwith 25 size
int index = 0;
while (number > 0)
{
binary[index++] = number % 2; dividing the number with 2 and storing the mod value in the array
number = number / 2; until the number becomes eual or less than 0
}
for (int i = index - 1; i >= 0; i--)
{
System.out.print(binary[i]); printing from most to least significant bit
}
}
} 23%2=1 1
23/2=11
11%2=1 1
A 11101 11/2=5 print
5%2=1 1
B 10111 5/2=2
2%2=0 0
2/2=1
C 11001 1%2=1 1

D Compilation Error or Runtime Error


82.
What will be the output of the following program?
class variables
{
int a;
static int b;
variables()
{
b++;
}
public void showData()
{
System.out.println(a + " " + b);
}
}
class variablesTesting
{
public static void main(String args[])
{
variables s1 = new variables(); new obj created....calls the constructor which incriments b by 1 as b is
s1.showData(); print a=0 b=1 static variable it will retain its value...now b=1
variables s2 = new variables(); new obj created....calls the constructor which incriments b by 1 as b is
s2.showData(); print a=0 b=2 static variable it will retain its value...now b=2
variables.b++; b=3 but not printed
}
}

01
A
02

11
B
12

22
C
33

12
D
34

E Compilation Error or Runtime Error


83.
What will be the output of the following program? Assume that we are using JDK1.7?
public class Select {
public static void choose(String str) {
switch (str) {
case "M" :
System.out.print("Merit "); no break....so continue until break found
case "C" :
System.out.print("Campus");
break;
case "W" :
System.out.print("Welcome ");
break;
case "T" :
System.out.print("To ");
break;
case "H" :
System.out.println();
default :
System.out.println("Default");
break;
}
}
public static void main(String arg[]) {
Select.choose("W");
Select.choose("M");
Select.choose("w"); no similar case default executed
}
}

A Merit Campus

B Welcome Merit CampusDefault

Welcome Merit Campus


C
Default

D Welcome Merit Campus Default

E Compilation Error or Runtime Error


84.
What is the output of the following program?
public class Dhoni {
static Dhoni dhoni;
public static void main(String... k) {
Animal a;
System.out.print((a = new Cat()).prey().getClass().getSimpleName() + "
");
System.out.print((a = new Rabbit()).prey().getClass().getSimpleName()
+ " ");
System.out.print((a = new Lion()).prey().getClass().getSimpleName() +
" ");
}
}
abstract class Animal {abstract Animal prey(); } not sure
class Cat extends Animal {
Cat prey() { // LINE B
return this;
}
}
class Rabbit extends Animal { Animal prey() { return this; } }
abstract class Lion extends Animal { // LINE A
Animal prey() { return this; }
}

A Cat Rabbit Lion


B Cat Animal Animal

Compilation Error at LINE A, since a class with no abstract methods can


C
not be declared as abstract

Compilation Error at LINE B, since overridden methods return type can


D
not be changed

E Compilation Error because of some other reason


85.
What is the output of the following program?
public class Krish {
static char c = 70;
public static void main(String[] args) {
Krish krish = new Krish();
System.out.print(c + " "); 70...AsccII code F
krish(c);
System.out.println(c); c not changed...so 70...AsccII code F
}
static void krish(char c) {
int cr = c; 70
cr = cr * 2; 180
cr = cr + c - 20; 190....so AscII code 3/4
c = (char) cr; all operation are done on local variable....so static global var will remain unchanged
}
}

A F 50

B f 50

C Ff

D FF

E ff

F 70 F
86.
What is the output of the following program?
public class ClazzTest {
static MyClass o1 = new MyClass(100); static method are called first....new obj will be created and it will
MyClass o2 = new MyClass(200); run its constractor which prints 100
public static void main(String[] args) { after static method main method is ran.....so new obj will be
new MyClass(300); created and it will run its constractor which prints 300
new MyClass();
MyClass o2 = new MyClass(200); not rum because it is out side main
method
}
static MyClass o5 = new MyClass(400); static method are called first....new obj will be created and it will
} run its constractor which prints 400
class MyClass {
public MyClass() { }
public MyClass(int i) { System.out.print(i + " "); }
}

A 100 300 400

B 100 200 300 400

C 100 400 200 300

D 100 400 300

E Compilation Error
87.
What is the output of the following program?
public class VariableTest {
static int i;
final double d = 58.3395;
int a;
public static void main(String[] args) {
VariableTest ob = new VariableTest();
System.out.println(ob.a++ + +ob.d + ++VariableTest.i);// LINE A
}
}

A 58.3395

B 60.3395
0+58.3395+1 here ob.a++ so first arethmatic operation will happen then ob.a will incriment
C 59.3395 by 1.....here ++VariableTest.i so first VariableTest.i will incriment by 1 then
arethmatic operation will happen
D null

E Compilation Error at LINE A


88.
What will be the output of the following program?
public class Write extends Read {
static Learn parent;
static Write child;
public static void main(String args[]) {
Write parent = new Write();
Write child = new Write();
System.out.println(checkBool1(parent instanceof Learn));
System.out.println(checkBool2(parent instanceof Write));
System.out.println(checkBool3(child instanceof Learn));
}
private static boolean checkBool1(boolean b1) {
return parent instanceof Learn;
}
private static boolean checkBool2(boolean b2) {
return child instanceof Learn;
}
public static boolean checkBool3(boolean b3) {
return parent instanceof Write;
}
}
interface Learn { }
class Read implements Learn { }

true
A true
true

true
B false
true

false
C true
false

false
not sure
D false
false

E Some other output

F Compilation Error or Runtime Error


89.
Which of the following instantiated objects defined in the instantiatedTest() method
are valid instantiations?
public class ValidOrNot {
public class X { }
public static class Y { }
public void instantiatedTest() {
new ValidOrNot().new X(); //M
new ValidOrNot.Y(); //N
new X(); //O
new ValidOrNot.X(); //P
}
}

A Only M not sure

B Only O

C M and O

D N and O

E M, N, O and P
90.
What will be the output of the following program?
class Note {
String n1 = "n1";
String n2 = "n2";
}
public class Book extends Note {
String n1 = "n3";
public static void main(String args[]) {
Book n = new Book(); obj of book
Note b = n; obj of note
System.out.println(n.n1 + n.n2 + b.n1 + b.n2);
} n3 n2 n1 n2
} from from from from
book note note note

A n1n2n1n2

B n3n2n3n2

C n3n2n1n2

D Some other output

E Compilation Error or Runtime Error


91.
What will be the output of the following program?
public class InnerC {
public static void main(String args[]) {
InnerC q = new InnerC(); new obj of innerc
q.myMethod(); calling mymethod using obj q......prints Welcome to the JAVA world
MyNest.main("Hello MeritCampus"); calling static method....prints Hello MeritCampus
}
void myMethod() {
System.out.println("Welcome to the JAVA world");
}
static class MyNest {
public static void main(String st) {
System.out.println(st);
}
}
}

Welcome to the JAVA world


A
Hello MeritCampus

B Welcome to the JAVA world

Welcome to the JAVA world


C Welcome to the JAVA world
Hello MeritCampus

D Compilation Error or Runtime Error


92.
What will be the output of the following program?
public class Facts {
public static void main(String[] args) {
System.out.println(Fact.fact(5)); compile error.....because Fact is not a static class...but here non-
} static method fact(int) is referenced from a static context....we are
} referencing it as static
class Fact {
int fact(int n) {
return n > 1 ? (fact(n - 1) * n) : 1;
}
}

A 120

B 1

C Some other output

D Compilation Error or Runtime Error


93.
What will be the output of the following program?
public class Island {
static String i = "hai";
public static void main(String[] args) {
Island i2 = new Island();
Island i3 = new Island();
Island i4 = new Island();
i2.i = "hello";
i3.i = "good";
i4.i = "evening";
System.out.print(i2.i+"-"); i is static...last chang value is evening....prints evening-evening-evening-
System.out.print(i3.i+"-"); evening
System.out.print(i4.i+"-");
System.out.println(Island.i);
i2 = null;
i3 = null;
i4 = null;
System.out.print(i2+"-"); i2,i3,i4 are assigned null so prints null-null-null
System.out.print(i3+"-");
System.out.print(i4);
}
}

hai-hai-hai-hai
A
null-null-null

hello-good-evening-hai
B
null-null-null

evening-evening-evening-evening
C
null-null-null

D Some other output

E Compilation Error or Runtime Error


94.
What will be the output of the following program?
public class ElectricityBill {

static double surcharge = 50.0;

public static void main(String[] args) {


ElectricityBill eb = new ElectricityBill();
System.out.print(eb.amount(500, 200) + "~");
System.out.print(eb.amount(600, 500) + "~");
System.out.print(eb.amount(1000, 600));
}

int amount(int current, int previous) {


int difference = current - previous;
double charges = surcharge;
if (difference > 300) {
charges += (difference - 300) * 6.0;
difference = difference - 300;
}
if (difference > 100) {
charges += (difference - 100) * 4.5;
difference = difference - 100;
}
charges += difference * 3.0;
return (int) charges;
}
}

A 50~50~650

B 1250~350~1850

C 1550~350~950

D Some other output or Compilation Error


95.
What will be the output of the following program?
class Dog {
public static void bark() {
System.out.print("Bow ");
}
}
class Cat extends Dog {
public static void bark() {
}
}
public class Fight {
public static void main(String args[]) {
Dog do = new Dog(); so is a builtin syntax....cannot use it as obj name
Dog ca = new Cat();
Dog.bark();
ca.bark();
}
}

A Bow Bow

B Bow Bow Bow

C Bow Bow Bow Bow

D Some other output

E Compilation Error or Runtime Error


96.
What will be the output of the following program?
public class IssueRises {
public String myIssue = "Issue not fixed";
public void IssueRises() { not executed as it is not constructor....and not called from main method
myIssue = "Issue is Fixed";
}
public static void main(String[] args) {
IssueRises ir = new IssueRises(); obj created
System.out.println(ir.myIssue); myIssue = "Issue not fixed" initialized
}
}

A Issue not fixed

B Issue fixed

C Some other output

D Compilation Error or Runtime Error


97.
What will be the output of the following program?
public class MyObject {
public static void main(String[] args) {
MyObject.myStaticMethod();
System.out.println(MyObject.myStaticMethod());
}

public String memberVar; Here memberVar is not a static variable


static private String memberStaticVar;
we are assigning a value to the non static variable inside
static public String myStaticMethod() {
static method....which is not possible
memberVar = "Value";
pssible solution : new MyObject().memberVar="Value"
memberStaticVar = "Value";
we can use this way to assign a value to a non static
MyObject obj = new MyObject();
variable inside static method
obj.memberVar = "Value";
System.out.println("Have a nice to Participants");
return ("No Error");
}
}

Have a nice to Participants


Have a nice to Participants
A
Have a nice to Participants
No Error

Have a nice to Participants


B Have a nice to Participants
No Error

Have a nice to Participants


C
No Error
D Some other output

E Compilation Error or Runtime Error


98.
What will be the output of the following program?
public class Bowler {
int runs = 81;
public static void main(String[] args) {
new Bowler().go();
}
int balls = 75;
void go() {
dec(--balls); dec(74) global ball value =74
System.out.print(runs + ","); print 81 global run value =81
System.out.print(balls + ","); print 74 global ball value =74
}
void dec(int balls) {
balls -= 20; local ball=54
System.out.print(balls + ","); print 54 local bal value
}
}

A 82,54,74,

B 54,81,74,

C 82,55,74

D 54,82,74

E Compilation Error or Runtime Error.


99.
What will be the output of the following program?
public class IDontKnow {
int calc(int[]... numbs) {
int sum = 0, b, c;
for (b = 0; b < numbs.length; b++) { not sure
for (c = 0; c < numbs[b].length; c++) {
sum += numbs[b][c];
}
}
return (sum);
}
public static void main(String args[]) {
IDontKnow numbers = new IDontKnow(); new obj created
int sum = 1;
sum = numbers.calc(new int[]{10, 20, 30, 40}, new int[]{40, 50, 60});
System.out.println("The sum of the numbers is: " + sum);
}
}

A The sum of the numbers is: 25

B The sum of the numbers is: 250

C The sum of the numbers is: 251

D Compilation Error or Runtime Error


100.
What will be the output of the following program?
public class IDontKnow {
int calc(int[]... numbs) {
int sum = 0, i, j;
for (i = 0; i < numbs.length; i++) {
for (j = 0; j < numbs[i].length; j++) {
sum += numbs[i][j];
} not sure
}
return (sum);
}
public static void main(String args[]) {
IDontKnow numbers = new IDontKnow();
int sum = 1;
sum = numbers.calc(new int[]{10, 20, 30, 40}, new int[40]);
System.out.println("The sum is : " + sum);
}
}

A The sum is : 100

B The sum is : 140

C The sum is : 141

D Compilation Error or Runtime Error

You might also like