15. //Predict the Output
public class Main
{
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(" 0 ");
case i-1: System.out.print(" 1 ");
case i-2: System.out.print(" 2 ");
}
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
16. 2 1 0 1 0 0
Question 7
A)
0 1 2 1 2 2
B)
2 1 2 0 1 2
C)
Compilation error
D)
17. //Predict the Output
public class Final
{
int a = 30;
public static void main(String[] args)
{
final int assign;
Final b = new Final();
process(b);
System.out.println(b.a);
process(b);
assign = b.a;
System.out.println(assign);
}
public static void process(Final a)
{
a.a = a.a + 5;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
21. //Predict the Output
public class Result
{
public static void main(String args[])
{
B b = new B();
System.out.println("x = " + b.getResult(0, 1));
}
}
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; }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
22. x = 1
Question 10
A)
x = 0
B)
Runtime error
C)
Compilation error
D)
23. //Predict the Output
public final class FinalPersonClass {
private final String name;
private final int age;
public FinalPersonClass(final String name, final int age){
super();
this.name = name;
this.age = age;
}
public int getAge() {
return age;
}
public String getName(){
return name;
}
public static void main(String[] args) {
FinalPersonClass fpc = new FinalPersonClass("Arun", 24);
FinalPersonClass fc = new FinalPersonClass("Kiran", 25);
System.out.println(fpc.name + " - " + fpc.age);
System.out.println(fc.getName() + " - " + fc.getAge());
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
24. Arun - 24
Kiran - 25
Question 11
A)
Arun - 24
Arun - 24
B)
Kiran – 25
Kiran - 25
C)
Compilation error
D)
25. //Predict the Output
public class Circle
{
private double radius;
public Circle(double radius)
{
radius = radius;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
26. The program does not compile because Circle does not have a default
constructor
Question 12
A)
The program will compile, but we cannot create an object of
Circle with a specified radius. The object will always have radius 0.
B)
The program has a compilation error because it does not have a main
method.
C)
The program has a compilation error because we cannot assign radius to
radius.
D)
27. //Predict the Output
public class Drive
{
static int i = demo();
static
{
System.out.print(i);
}
Drive()
{
System.out.print("String2");
}
public static void main(String args[])
{
System.out.print("String1");
}
static int demo()
{
System.out.print("Drive");
return 10;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
29. //Predict the Output
class Static{
static int x;
static int y;
void add(int a, int b){
x = a + b;
y = x + b;
}
}
public class static_use
{
public static void main(String args[])
{
Static obj1 = new Static();
Static obj2 = new Static();
int a = 2;
obj1.add(a, a + 1);
obj2.add(5, a);
System.out.println(obj1.x + " " + obj2.y);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
35. //Predict the Output
public class Main
{
void MethodOfClassA()
{
System.out.println("Method of ClassA");
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
36. Main method not found in class
Question 17
A)
Method of ClassA
B)
It will throws an Error
C)
Methods of ClassA prints infinite time
D)
37. //Predict the Output
public class A
{
private : int sum(int x, int y)
{
return x+y;
}
public: A()
{
}
A(int x, int y)
{
cout<<sum(x,y);
}
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
38. Constructor will give error if float values are passed
Question 18
A)
Constructor can be created with zero argument
B)
Constructor prints sum, if two parameters are passed with object
creation
C)
Constructor will take 0 as default value of parameters if not passed
D)
39. Given a method in a protected class, what access modifier do you use to
restrict access to that method to only the other members of the same class?
private
Question 19
A)
protected
B)
static
C)
final
D)
40. If class A has add() function with protected access, and few other members
in public.Then class B inherits class A privately. Will the user will not be able
to call _________ from the object of class B.
Any member of class A
Question 20
A)
Any function of class A
B)
The add() function of class A
C)
Private, protected and public members of class A
D)
41. //Predict the Output
class Methods
{
private double num = 100;
private int square(int a)
{
return a*a;
}
}
public class Main
{
public static void main(String args[])
{
Methods obj = new Methods();
System.out.println(obj.num);
System.out.println(obj.square(10));
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
43. Question 22
Which is valid in a class that extends class A?
class A
{
protected int method1(int a, int b)
{
return 0;
}
}
44. private int method1(int a, int b) { return 0; }
Question 22
A)
public int method1(int a, int b) {return 0; }
B)
public short method1(int a, int b) { return 0; }
C)
static protected int method1(int a, int b) { return 0; }
D)
45. //Predict the Output
public class Main
{
public static void main(String args[])
{
class Goto
{
public int i = 3;
}
Object t1 = (Object)new Goto();
Goto obj = (Goto)t1;
System.out.println("i = " + obj.i);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
46. i = 1
Question 23
A)
i = 0
B)
i = 3
C)
Compilation error
D)
47. //Predict the Output
public class Methods
{
public int Method()
{
static int i = 0;
i++;
return i;
}
public static void main(String args[])
{
Methods test = new Methods();
test.Method();
int j = test.Method();
System.out.println(j);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
55. //Predict the Output
class Super
{
public Integer getLength()
{
return new Integer(4);
}
}
public class Sub extends Super
{
public Long getLength()
{
return new Long(5);
}
public static void main(String[] args)
{
Super obj = new Super();
Sub sub = new Sub();
System.out.println(obj.getLength().toString() + "," +
sub.getLength().toString());
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22