Java Output
Java Output
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;
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.
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;
System.out.println(result);
}
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;
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;
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;
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;
f = modify(f); forcefully assigned to final obj f.....so compilation error....rest of the code not executed
f.process();
}
return f2;
}
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;
System.out.println(assign);
System.out.println(process(result));
}
int process(int a)
{
return a + 5;
}
}
20
A
25
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
}
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
A MERIT CAMPUS
B CAMPUS MERIT
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;
A 012
B 012122
for z = 1
for z = 3
A 012 no case is matched....nothing printed
B 012122
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
}
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 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();
}
}
Final class
B Inter Class
Sub class extends the Final Base class
Final class
C Inter Class
Final class
A [1,3]:Blue
B [2,4]:Blue
C [3,1]:Blue
Derived
B
Derived
Base
Derived
C
Base
Derived
D Derived
A Output=99
B Output=100
C Output=101
A JAVA
JAVA
B
java
JAVA
C
JAVA
Kiran - 25
A
Kiran - 25
Oleti - 24
B
Oleti - 24
Oleti - 24
C
Kiran - 25
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;
}
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
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();
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
A 41
B 42
C 50
D 51
B 00
C 0 40
D 40 80
A i = 0, i = 5
B i = 5, i = 5
C Compilation Error
D NullPointerException
A false false
B true true
C false true
D true false
A 317
B 615
C 617
D 315
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
B 50
C 59
D 49
A -GoodBoy--GoodGirl--7--7-
B -GoodBoy--GoodGirl--33--11-
C -GoodBoy--GoodGirl--33--33-
D -GoodBoy--GoodGirl--40--40-
execute
execute, execute
A
execute, execute, execute
execute, execute, execute, execute
execute
execute, execute
B
execute, execute, execute
execute
execute
C
execute
execute
execute
A 0~0,1~0,2~0,
B 0~0,1~1,2~2,
D Compilation Error
A 5@16@10@16@
B 5@16@9@16@
C 16@111@16@76@
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,},
A 25 28 56 109
B 0369
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;
}
}
true
false
A
9.5
42
false
false
B
9.5
42
true
ture
C
9.5
42
A true true
B false false
C true false
D false true
A final result is 3
B final result is 2
C final result is 0
A HaiHelloWelcome
B WelcomeHelloHai
C HelloHaiWelcome
D HaiWelcomeHello
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
A 5
B 1000
C 5000
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 @#%&@#~$@#
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;
A i value is 0
B i value is 5
D Compilation Error
55.
Which of the following false java statement?
B A cannot be static.
A Docomo
B Mobile is : Airtel
C Mobile is : Docomo
A 9909
B a99b18a9c9e
C a90b0a9c-9e
Class ClassTest
A Class A
Class B
Class ClassTest
B
Class A
Class ClassTest
C
Class B
Class A
D
Class B
D Program execute =
A %&@#~$@#
B @%&@#~$#
C %&-$@#@#
D @#%&@#~$@#
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;
}
}
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
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
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;
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) {
this(a, b, c, a + b + c);
}
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;
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
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...
A X only
B Y only
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
A 77
B 7 14
C 14 0
D 14 14
D 1234
A 527483
B 54783
C 4782
D 27
E 2
01
A
02
11
B
12
22
C
33
12
D
34
A Merit Campus
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 + " "); }
}
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
true
A true
true
true
B false
true
false
C true
false
false
not sure
D false
false
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
A 120
B 1
hai-hai-hai-hai
A
null-null-null
hello-good-evening-hai
B
null-null-null
evening-evening-evening-evening
C
null-null-null
A 50~50~650
B 1250~350~1850
C 1550~350~950
A Bow Bow
B Issue fixed
A 82,54,74,
B 54,81,74,
C 82,55,74
D 54,82,74