Java SE 8 Question Bank
Java SE 8 Question Bank
Java SE 8 Question Bank
Topic-3: Arrays
Topic-7: Flow-Control
Topic-9: OOPs
Topic-10: Constructors
Topic-14: ArrayList
1 https://www.youtube.com/durgasoftware
OCJA
Answer: B and D
Answer: C
2 https://www.youtube.com/durgasoftware
OCJA
A) int to byte
B) byte to int
C) float to double
D) double to float
E) None of the above
Answer: B, C
A) int to byte
B) byte to int
C) float to double
D) double to float
E) None of the above
Answer: A, D
int i =100;
float f = 100.100f;
double d = 123;
Answer: A,D,F
Q4. In which of the following cases we will get Compile time error?
A) float f =100F;
B) float f =(float)1_11.00;
C) float f =100;
3 https://www.youtube.com/durgasoftware
OCJA
D) double d = 203.22;
float f = d;
E) int i =100;
float f=(float)i;
Answer: D
Answer: A
4 https://www.youtube.com/durgasoftware
OCJA
Topic-3: Arrays
Q1. Consider the folliwng code
A) 4
6
B) 4
4
C) 6
4
D) 6
6
Answer: A
int[] x= {10,20,30,40,50};
x[2]=x[4];
x[4]=60;
A) 10,20,30,40,50
B) 10,20,50,40,50
5 https://www.youtube.com/durgasoftware
OCJA
C) 10,20,50,40,60
D) 10,20,30,40,50
Answer: C
A) 10:20:30:40:50:
B) 10:20:30:
C) Compilation fails
D) ArrayIndexOutOfBoundsException at runtime
Answer: A
6 https://www.youtube.com/durgasoftware
OCJA
Which code fragment required to insert at Line-1 to produce output 10:20
B) int[] x;
x = int[2];
D) int x[2];
Answer: A
1) class Student
2) {
3) int rollno;
4) String name;
5) public Student(int rollno,String name)
6) {
7) this.rollno=rollno;
8) this.name=name;
9) }
10) }
11) public class Test
12) {
13) public static void main(String[] args)
14) {
15) Student[] students ={
16) new Student(101,"Durga"),
17) new Student(102,"Ravi"),
18) new Student(103,"Shiva"),
19) new Student(104,"Pavan")
20) };
21) System.out.println(students);
22) System.out.println(students[2]);
23) System.out.println(students[2].rollno);
24) }
25) }
A) students
Shiva
103
7 https://www.youtube.com/durgasoftware
OCJA
B) [LStudent;@61baa894
Shiva
103
C) [LStudent;@61baa894
Student@66133adc
103
D) [LStudent;@61baa894
Pavan
103
Answer: C
A) grid[3][1]='X';
B) grid[0][2]='X';
C) grid[1][3]='X';
D) grid[2][0]='X';
E) grid[1][2]='X';
Answer: D
1) class Test
2) {
3) public static void main(String[] args)
4) {
5) String[] s = new String[2];
6) int i =0;
7) for(String s1 : s)
8) {
9) s[i].concat("Element "+i);
10) i++;
11) }
8 https://www.youtube.com/durgasoftware
OCJA
12) for(i=0;i<s.length;i++)
13) {
14) System.out.println(s[i]);
15) }
16) }
17) }
B) null Element 0
null Element 1
C) null
null
D) NullPointerException
Answer: D
Note: On null,if we are tryint to apply any operation then we will get NullPoinerException
1) class Test
2) {
3) public static void main(String[] args)
4) {
5) int[][] n = new int[1][3];
6) for(int i =0; i< n.length; i++)
7) {
8) for (int j=0;j>n[i].length ;j++ )
9) {
10) num[i][j]=10;
11) }
12) }
13) }
14) }
Which option represents the state of the array after successful completion of outer for
loop?
A) n[0][0]=10;
n[0][1]=10;
n[0][2]=10;
9 https://www.youtube.com/durgasoftware
OCJA
B) n[0][0]=10;
n[1][0]=10;
n[2][0]=10;
C) n[0][0]=10;
n[0][1]=0;
n[0][2]=0;
D) n[0][0]=10;
n[0][1]=10;
n[0][2]=10;
n[1][0]=0;
n[1][1]=0;
n[1][2]=0;
n[1][3]=0;
Answer: A
1) class Student
2) {
3) String name;
4) public Student(String name)
5) {
6) this.name=name;
7) }
8) }
9) public class Test
10) {
11) public static void main(String[] args)
12) {
13) Student[] students = new Student[3];
14) students[1]= new Student("Durga");
15) students[2]= new Student("Ravi");
16) for(Student s : students)
17) {
18) System.out.println(s.name);
19) }
20) }
21) }
10 https://www.youtube.com/durgasoftware
OCJA
What is the result?
A) Durga
Ravi
B) null
Durga
Ravi
C) Compilation Fails
D) ArrayIndexOutOFBoundsException
E) NullPointerException
Answer: E
11 https://www.youtube.com/durgasoftware
OCJA
Answer: C
Explanation: In the above example, the variable myStr, which is declared inside try block is not
available outside of try block. Hence while printing, instance variable myStr will be considered.
12 https://www.youtube.com/durgasoftware
OCJA
6) try
7) {
8) String myStr=s;
9) myNum=Integer.parseInt(myStr);
10) }
11) catch(NumberFormatException e)
12) {
13) System.err.println("Error");
14) }
15) System.out.println("myStr: "+myStr+" ,myNum: "+myNum);
16) }
17) public static void main(String[] args)
18) {
19) Test t = new Test();
20) t.doStuff("9009");
21) }
22) }
Answer: D
Explanation: myStr is local variable of try block and we cannot access outside of try block.
1) class Test
2) {
3) int x =10;
4) static int y = 20;
5) public static void main(String[] args)
6) {
7) Test t1 =new Test();
8) Test t2 =new Test();
9) t1.x=100;
10) t1.y=200;
11) t2.x=300;
12) t2.y=400;
13) System.out.println(t1.x+".."+t1.y+".."+t2.x+".."+t2.y);
14) }
15) }
A) 100..400..300..400
B) 100..400..100..400
C) 200..400..300..400
13 https://www.youtube.com/durgasoftware
OCJA
D) 100..200..300..400
Answer: A
Answer: C
14 https://www.youtube.com/durgasoftware
OCJA
17) t1.modify();
18) t2.modify();
19) System.out.println(t1.count+".."+t2.count);
20) }
21)
22) }
Answer: A
1) class Test
2) {
3) int count;
4) public static void display()
5) {
6) count++;//Line-1
7) System.out.println("Welcome Visit Count:"+count);//Line-2
8) }
9) public static void main(String[] args)
10) {
11) Test.display();//Line-3
12) Test.display();//Line-4
13) }
14) }
Answer: C
15 https://www.youtube.com/durgasoftware
OCJA
Q7. Consider the code
B) 200:300
200:300
C) 300:0
0:300
D) 300:300
200:300
Answer: A
16 https://www.youtube.com/durgasoftware
OCJA
10) b=3;
11) h=4;
12) p=0.5;
13) }
14) area=p*b*h;// Line-2
15) System.out.println(area);
16) }
17) }
Answer: D
1) class Demo
2) {
3) int ns;
4) static int s;
5) Demo(int ns)
6) {
7) if(s<ns)
8) {
9) s=ns;
10) this.ns=ns;
11) }
12) }
13) void display()
14) {
15) System.out.println("ns = "+ns+" s = "+s);
16) }
17) }
18) public class Test
19) {
20) static int x;
21) int y;
22) public static void main(String[] args)
23) {
24) Demo d1= new Demo(50);
25) Demo d2= new Demo(125);
26) Demo d3= new Demo(100);
27) d1.display();
28) d2.display();
29) d3.display();
17 https://www.youtube.com/durgasoftware
OCJA
30) }
31) }
A) ns = 50 s = 125
ns = 125 s = 125
ns = 0 s = 125
B) ns = 50 s = 125
ns = 125 s = 125
ns = 100 s = 125
C) ns = 50 s = 50
ns = 125 s = 125
ns = 0 s = 0
D) ns = 50 s = 125
ns = 125 s = 125
ns = 100 s = 100
Answer: A
A) 400 200
B) 200 200
C) 400 400
D) Compilation Fails
Answer: A
18 https://www.youtube.com/durgasoftware
OCJA
Q11. Consider the code
A) Answer=0
B) Invalid Calculation
C) Compiation Fails at Line-1
D) Compiation Fails at Line-2
E) Compiation Fails at Line-1 and Line-2
Answer: E
19 https://www.youtube.com/durgasoftware
OCJA
13) {
14) Test t = new Test();
15) t.print();
16) }
17) }
A) c =
b = false
f = 0.0
B) c = null
b = false
f = 0.0
C) c = 0
b = false
f = 0.0
D) c =
b = true
f = 0.0
Answer: A
Parameter Passing
Q13. Consider the code
20 https://www.youtube.com/durgasoftware
OCJA
17) }
Output:
Inside Method:110..210
After Completing Method:100..200
A) 400 200
B) 200 200
C) 400 400
D) Compilation Fails
Answer: A
21 https://www.youtube.com/durgasoftware
OCJA
15) int i =3, j= 5;
16) Test t = new Test(i,j);
17) System.out.println(i+"..."+j);
18) }
19) }
A) 9...25
B) 0...0
C) 3...5
D) Compilation Fails
Answer: C
1) class Demo
2) {
3) int x;
4) int y;
5) };
6) public class Test
7) {
8) public void m1(Demo d)
9) {
10) d.x=888;
11) d.y=999;
12) }
13) public static void main(String[] args)
14) {
15) Demo d1 = new Demo();
16) d1.x=10;
17) d1.y=20;
18) Test t = new Test();
19) t.m1(d1);
20) System.out.println(d1.x+"..."+d1.y);
21) }
22) }
Output: 888...999
Explanation:
In the above example we are passing Demo object reference as argument to method m1().Inside
method m1(),we are changing the state of the object. These changes will be reflected to the caller.
22 https://www.youtube.com/durgasoftware
OCJA
Q17. Consider the code
1) class Product
2) {
3) double price;
4) }
5) public class Test
6) {
7) public void updatePrice(Product p,double price)
8) {
9) price=price*2;
10) p.price=p.price+price;
11) }
12) public static void main(String[] args)
13) {
14) Product prt = new Product();
15) prt.price=200;
16) double newPrice=100;
17)
18) Test t = new Test();
19) t.updatePrice(prt,newPrice);
20) System.out.println(prt.price+"...."+newPrice);
21) }
22) }
Answer: C
Explanation:
In the above example, we are passing Product object reference as argument to updatePrice()
method and within the method we are changing the state of object. These changes will be
reflected to the caller.
23 https://www.youtube.com/durgasoftware
OCJA
9) }
10) public void print()
11) {
12) System.out.print(x+":"+y+":");
13) }
14) public static void main(String[] args)
15) {
16) Test t1=new Test();
17) t1.x=100;
18) t1.y=200;
19)
20) Test t2 = new Test();
21) t2.doStuff(t1.x,t1.y);
22) t1.print();
23) t2.print();
24) }
25) }
A) 100:200:100:0:
B) 100:0:100:0:
C) 100:200:100:200:
D) 100:0:200:0:
Answer: A
24 https://www.youtube.com/durgasoftware
OCJA
What is the result?
A) a:e
o:o
B) e:e
i:o
C) a:e
i:o
D) e:e
o:o
Answer: A
25 https://www.youtube.com/durgasoftware
OCJA
Topic-5: Main Method and Command Line Arguments
Q1. Which one of the following code examples uses valid java syntax?
A)
B)
C)
D)
Answer: A
26 https://www.youtube.com/durgasoftware
OCJA
Q2. Given the code from the Test.java file:
A) javac Test
java Test Durga
C) javac Test.java
java Test Durga
D) javac Test.java
java Test.class Durga
Answer: C
27 https://www.youtube.com/durgasoftware
OCJA
What is the result?
A) int[] main 1
B) Object[] main 1
C) String[] main 1
D) Compilation Fails
E) An Exception raises at runtime
Answer: C
28 https://www.youtube.com/durgasoftware
OCJA
System.out.println("5 + 2 = "+4+3);
System.out.println("5 + 2 = "+(4+3));
A) 5 + 2 = 43
5 + 2 = 43
B) 5 + 2 = 7
5+2=7
C) 5 + 2 = 7
5 + 2 = 43
D) 5 + 2 = 43
5+2=7
Answer: D
A) Result A:45
Result B:45
B) Result A:45
Result B:9
C) Result A:9
Result B:45
D) Result A:9
Result B:9
Answer: A
29 https://www.youtube.com/durgasoftware
OCJA
Q3. Consider the following code
1) class Test
2) {
3) public static void main(String[] args)
4) {
5) int x =100;
6) int a = x++;
7) int b = ++x;
8) int c =x++;
9) int d=(a<b)?(a<c)?a:(b<c)?b:c;
10) System.out.println(d);
11) }
12) }
Answer : E
30 https://www.youtube.com/durgasoftware
OCJA
D) Hi Durga 2:1
Answer: B
1) class Test
2) {
3) public static void main(String[] args)
4) {
5) if(x++<10)
6) {
7) System.out.println(x+" Hello India");
8) }
9) else
10) {
11) System.out.println(x+" Hello DURGASOFT");
12) }
13) }
14) }
A) 10 Hello India
B) 10 Hello DURGASOFT
C) 9 Hello India
D) Compilation fails
31 https://www.youtube.com/durgasoftware
OCJA
Answer: A
A) OCJA
B) OCJP
C) Compilation Fails
D) NullPointerException is thrown at runtime
Answer : B
32 https://www.youtube.com/durgasoftware
OCJA
8) this.name=name;
9) }
10) }
Answer: C
A) String s3=s2;
if(s1==s3)
B) if(s1.equalsIgnoreCase(s2))
33 https://www.youtube.com/durgasoftware
OCJA
C) String s3=s2;
if(s1.equals(s3))
D) if(s1.toLowerCase() == s2.toLowerCase())
Answer: B
Answer: C
34 https://www.youtube.com/durgasoftware
OCJA
13) }
14) }
javac Test.java
java Test Durga
A) Success
B) Failure
C) Compilation fails
D) Runtime Exception
Answer: B
A) First Level
Second Level
B) Second Level
First Level
C) First Level
First Level
35 https://www.youtube.com/durgasoftware
OCJA
D) Second Level
Second Level
Answer : B
1) String s="Color";
2) String result=null;
3) if(s.equals("Color"))
4) {
5) result="Blue";
6) }
7) else if(s.equals("Wall"))
8) {
9) result="Regular";
10) }
11) else
12) {
13) result="No Result";
14) }
Answer: D
If the value of the quantity variable is greater than or equal to 90, discount=20
If the value of the quantity variable is between 80 and 90 , discount=10
36 https://www.youtube.com/durgasoftware
OCJA
Which two code fragments can be independently placed at Line-1 to meet the
requirements ?
B) discount=(quantity >= 90 ) ? 20 : 0;
discount=(quantity > 80 ) ? 10 : 0;
D)
Answer : A and C
37 https://www.youtube.com/durgasoftware
OCJA
Topic-7: Flow-Control
Q1. Consider the code
Answer: A
38 https://www.youtube.com/durgasoftware
OCJA
10) break;
11) case 20:
12) System.out.println("Twenty");
13) break;
14) }
15) }
16) }
Answer: A,B,F
39 https://www.youtube.com/durgasoftware
OCJA
Answer: A
A) Red
Blue
B) Green
Default
C) Default
D) Green
Answer: D
Answer : D
40 https://www.youtube.com/durgasoftware
OCJA
Comparison:
1. If we dont know the number of iterations in advanace then we should go for while loop.
2. If we want to execute loop body atleast once then we should go for do-while loop.
3. If we know the number of iterations in advance then we should use for loop.
4. To retrieve the elements of arrays and collections then we should use for-each loop.
Answer : B
41 https://www.youtube.com/durgasoftware
OCJA
9) System.out.print(x[i]+" ");
10) i++;
11) }
12) while (i<x.length -1);
13) }
14) }
Answer: B
A) 5 4 3 2 1 0
B) 5 4 3 2 1
C) 4 2 1
D) 5
E) Nothing is printed
Answer: D
42 https://www.youtube.com/durgasoftware
OCJA
7) x[1]= new int[]{2,4};
8) for(int[] x1: x)
9) {
10) for(int x2 :x1)
11) {
12) System.out.print(x2+" ");
13) }
14) System.out.println();
15) }
16) }
17) }
A) 2 4 6 8
24
B) 2 4
24
C) Compilation Fails
D) ArrayIndexOutOfBoundsException
Answer: A
43 https://www.youtube.com/durgasoftware
OCJA
What is the output?
A) Durga
Ravi
B) Durga
Ravi
null
C) Compilation Fails
D) ArrayIndexOutOfBoundsException
E) NullPointerException
Answer: E
A) 0
B) 1
C) 2
D) Compilation Fails
Answer: D
Note: In the above code if we take count++; outside of if block then output is 2 (Option C).
44 https://www.youtube.com/durgasoftware
OCJA
Q12. Consider the code
Answer: A
45 https://www.youtube.com/durgasoftware
OCJA
Which option can replace yyy to enable the code to print 135?
A) int i=0;i<=4;i++
B) int i=0;i<5;i+=2
C) int i=1;i<=5;i++
D) int i=1;i<5;i+=2
Answer: B
1) for(int i=1;i<2;i++)
2) {
3) for(int j =1; j<2;j++)
4) {
5) System.out.print(colors[i][j]+":");
6) }
7) }
B)
1) for(int i=0;i<2;+++)
2) {
3) for(int j =0; j<i;++j)
4) {
5) System.out.print(colors[i][j]+":");
6) }
7) }
C)
1) for(String c: colors)
2) {
46 https://www.youtube.com/durgasoftware
OCJA
3) for(String s: sizes )
4) {
5) System.out.print(s+":");
6) }
7) }
D)
1) for(int i=0;i<2;)
2) {
3) for(int j =0; j<2;)
4) {
5) System.out.print(colors[i][j]+":");
6) j++;
7) }
8) i++;
9) }
A)
1) do
2) {
3) i++;
4) }
5) while ( i>= size );
B)
1) while(i<size)
2) {
3) i++;
4) }
47 https://www.youtube.com/durgasoftware
OCJA
C)
1) do
2) {
3) i++;
4) }
5) while (i<size-1);
D)
1) do
2) {
3) i++;
4) }
5) while (i<= size);
E)
1) while(i<= size-1)
2) {
3) i++;
4) }
Answer: C
48 https://www.youtube.com/durgasoftware
OCJA
What is the result?
Answer : B
Answer : B
int[] x ={10,20,30,40};
A) Requirements 1,2 and 3 can be implemented by using the enhanced for loop
B) Requirements 1,2 and 3 can be implemented by using the standard for loop
49 https://www.youtube.com/durgasoftware
OCJA
C) Requirements 2 and 3 CANNOT be implemented by using the standard for loop
D) Requirement 1 can be implemented by using the enhanced for loop
E) Requirement 3 CANNOT be implemented by using either the enhanced for loop or the standard
for loop.
Answer: B,D
int[] x = {10,20,30,40,50};
Which two code fragments independently print each element of this array?
A)
1) for(int i : x)
2) {
3) System.out.print(x[i]+" ");
4) }
B)
1) for(int i : x)
2) {
3) System.out.print(i+" ");
4) }
C)
1) for(int i=0 : x)
2) {
3) System.out.print(x[i]+" ");
4) i++;
5) }
D)
E)
50 https://www.youtube.com/durgasoftware
OCJA
F)
Answer : B and E
Explanation:
Standard for loop is index based where as Enhanced for loop is Element based.
A) A B C D Done
B) A B C Done
C) A Done
D) Compilation fails
Answer : C
51 https://www.youtube.com/durgasoftware
OCJA
6) for(int i =0; i<s.length;i++)
7) {
8) for(int j =0; j<s[i].length;j++)
9) {
10) System.out.print(s[i][j]+" ");
11) if(s[i][j].equals("B"))
12) {
13) break;
14) }
15) }
16) continue;
17) }
18) }
19) }
A) A B C D E
B) A B C
C) A B D E
D) Compilation fails
Answer : C
A) 0 2 4 6
B) 0 2 4
C) 2 4 6
D) Compilation fails
Answer : B
52 https://www.youtube.com/durgasoftware
OCJA
Q23. Consider the following code
A) 97 98
99 100 null null null
B) 97 98
99 100 101 102 103
C) Compilation fails
53 https://www.youtube.com/durgasoftware
OCJA
Q24. Consider the following code
A) 1 2 3 4
12
B) 1 2
12
C) Compilation fails
Answer : A
54 https://www.youtube.com/durgasoftware
OCJA
Topic-8: Declarations and Access Modifiers
Part-1: Import statement and packages
Part-2: Access Modifiers (public,private,protected and default)
part-3: abstract modifier and interfaces
SalesMan.java:
package sales;
public class SalesMan{}
Product.java:
package sales.products;
public class Product{}
Market.java:
1) package market;
2) //Line-1
3) public class Market
4) {
5) SalesMan sm;
6) Product p;
7) }
Which code fragment when inserted at line 2, enables the code to compile?
A) import sales.*;
B) import java.sales.products.*;
C) import sales;
import sales.products;
D) import sales.*;
import products.*;
E) import sales.*;
import sales.products.*;
Answer : E
Note: Whenever we are importing a package, all classes and interfaces present in that package
are by default available but not sub package classes. Hence to use sub package class, compulsory
we should write import statement until sub package level.
55 https://www.youtube.com/durgasoftware
OCJA
Q2. Consider the code
1) package pack1;
2) public class A
3) {
4) int p;
5) private int q;
6) protected int r;
7) public int s;
8) }
Test.java:
1) package pack2;
2) import p1.A;
3) public class Test extends A
4) {
5) public static void main(String[] args)
6) {
7) A obj= new Test();
8) }
9) }
Answer: B
A)
B)
56 https://www.youtube.com/durgasoftware
OCJA
5) }
C)
D)
Answer: C
Q4. You are asked to develop a program for a shopping application, and you are given the
following information:
The application must contains the classes Book,JavaBook and PythonBook. The Book class is the
super class of other 2 classes.
Which definition of the Book class adds a valid layer of abstraction to the class hierarchy?
A)
B)
57 https://www.youtube.com/durgasoftware
OCJA
C)
D)
Answer: A
1) interface Interf
2) {
3) public void m1();
4) public void m2();
5) }
6) class A implements Interf
7) {
8) public void m1(){}
9) }
Which of the following changes individually will compile the code successfully?
Answer: A and B
1) interface Writable
2) {
3) public void writeBook();
4) public void setBookMark();
5) }
6) abstract class Book implements Writable //Line-1
7) {
58 https://www.youtube.com/durgasoftware
OCJA
8) public void writeBook(){}
9) //Line-2
10) }
11) class EBook extends Book //Line-3
12) {
13) public void writeBook(){}
14) //Line-4
15) }
C) At Line-2 insert
public abstract void setBookMark();
D) At Line-4 insert:
public void setBookMark(){}
Answer : D
X.java:
1) public class X
2) {
3) public void a(){}
4) int a;
5) }
Y.java:
1) public class Y
2) {
3) private int doStuff()
4) {
5) private int i =100;
6) return i++;
59 https://www.youtube.com/durgasoftware
OCJA
7) }
8) }
Z.java:
1) import java.io.*;
2) package pack1;
3) class Z
4) {
5) public static void main(String[] args)throws IOException
6) {
7) }
8) }
Answer: A
A.java:
1) package pack1;
2) public class A
3) {
4) }
B.java:
1) package pack1.pack2;
2) //Line-1
3) public class B
4) {
5) public void m1()
6) {
7) A a = new A();
8) }
9) }
60 https://www.youtube.com/durgasoftware
OCJA
C.java:
1) package pack3;
2) //Line-2
3) public class C
4) {
5) public static void main(String[] args)
6) {
7) A a = new A();
8) B b = new B();
9) }
10) }
Answer: A
61 https://www.youtube.com/durgasoftware
OCJA
Topic-9: OOPs
Exam Objectives:
1. Compare and contrast the features and components of Java such as: object orientation,
encapsulation, etc.
2. Describe inheritance and its benefits
3. Create methods with arguments and return values; including overloaded methods
4. Apply encapsulation principles to a class
5. Develop code that makes use of polymorphism; develop code that overrides methods;
differentiate between the type of a reference and the type of an object
6. Create and overload constructors; differentiate between default and user defined constructors
7. Use super and this to access objects and constructors
8. Determine when casting is necessary
Q1. Which three statements describe the object oriented featues of the java language?
Answer: B,C,E
Q2. What is the name of the Java concept that uses access modifiers to protect variables
and hide them within a class?
A. Encapsulation
B. Inheritance
C. Abstraction
D. Instantiation
E. Polymorphism
Ans: A
Explanation: This concept is data hiding, but that option is not available and hence we can choose
encapsulation
62 https://www.youtube.com/durgasoftware
OCJA
Q3. Which statement best describes encapsulation?
A. Encapsulation ensures that classes can be designed so that only certain fields and methods of
an object are accessible from other objects
B. Encapsulation enures that classes can be designed so that thier methods are inheritable
C. Encapsulation ensures that classes can be designed with some fields and methods declared as
abstract.
D. Encapsulation ensures that classes can be designed so that if a method has an argument X,any
subclass of X can be passed to that methods.
Answer: A
How should you write methods in ElectricAccount class at Line-1 so that the member variable bill
is always equal to the value of the member variable kwh multiplied by the member variable rate?
Any amount of electricity used by Customer (represented by an instance of the Customer class)
must contribute to the Customer's bill(represented by member variable bill) through the method
useElectricity() method. An instance of the customer class should never be able to tamper with or
decrease the value of the member variable bill?
A.
63 https://www.youtube.com/durgasoftware
OCJA
B.
C.
D.
Answer: C
Explanation:
64 https://www.youtube.com/durgasoftware
OCJA
An instance of the customer class should never be able to tamper with or decrease the value of
the member variable bill
If we pass -ve value for kwh then we can decrease the bill. To prevent this we should check kwh>0
Which two changes would encapsulation this class and ensure that the area field is always
equal to length*height,whenever Rectangle class is used?
Answer: B,F
65 https://www.youtube.com/durgasoftware
OCJA
10) {
11) public int stockOptions;
12) }
Which two options fail to compile when placed at Line 1 of the main method?
A. e.salary=50_000;
B. d.salary=80_000;
C. e.budget=2_00_000;
D. m.budget=1_00_000;
E. m.stockOption=500;
F. d.stockOption=1_000;
Answer: C and E
66 https://www.youtube.com/durgasoftware
OCJA
C. Make that method at Line-3 public
D. Make that method at Line-3 protected
E. Make that method at Line-4 public
Answer: C,D
Explanation: While overriding, we cannot reduce the scope of access modifier, but we can
increase the scope
Q8. Given:
Base.java:
1) class Base
2) {
3) public void test()
4) {
5) System.out.println("Base");
6) }
7) }
DerivedA.java:
DerivedB.java
1) class DerivedB extends DerivedA
2) {
3) public void test()
4) {
5) System.out.println("DerivedB");
6) }
7) public static void main(String[] args)
8) {
9) Base b1= new DerivedB();
10) Base b2= new DerivedA();
11) Base b3= new DerivedB();
12) b1=(Base)b3;
13) Base b4=(DerivedA)b3;
14) b1.test();
15) b4.test();
67 https://www.youtube.com/durgasoftware
OCJA
16) }
17)
18) }
A. Base
DerivedA
B. Base
DerivedB
C. DerivedB
DerivedB
D. DerivedB
DerivedA
Answer: C
Answer: C,D
Q10. Which three statements are true about the structure of a Java class?
Answer: C, D,F
68 https://www.youtube.com/durgasoftware
OCJA
Q11. Given:
1) class A
2) {
3) public void test()
4) {
5) System.out.print("A");
6) }
7) }
8) class B extends A
9) {
10) public void test()
11) {
12) System.out.print("B");
13) }
14) }
15) public class C extends A
16) {
17) public void test()
18) {
19) System.out.print("C");
20) }
21) public static void main(String[] args)
22) {
23) A a1 = new A();
24) A a2 = new B();
25) A a3 = new C();
26) a1.test();
27) a2.test();
28) a3.test();
29) }
30) }
A. AAA
B. ABC
C. AAB
D. ABA
Ans: B
Q12. Given:
69 https://www.youtube.com/durgasoftware
OCJA
5) System.out.println("Integer sum is:"+(x+y));
6) }
7) public static void sum(double x,double y)
8) {
9) System.out.println("double sum is:"+(x+y));
10) }
11) public static void sum(float x,float y)
12) {
13) System.out.println("float sum is:"+(x+y));
14) }
15) public static void sum(int x,int y)
16) {
17) System.out.println("int sum is:"+(x+y));
18) }
19) public static void main(String[] args)
20) {
21) sum(10,20);
22) sum(10.0,20.0);
23) }
24) }
Answer: A
70 https://www.youtube.com/durgasoftware
OCJA
11) }
A. 600
B. Compilation Fails at Line-1
C. Compilation Fails at Line-2
D. A ClassCastException is thrown at Line-1
E. A ClassCastException is thrown at Line-2
Answer: C
71 https://www.youtube.com/durgasoftware
OCJA
Topic-10: Constructors
Q1. Given:
1) class Vehicle
2) {
3) String type="4w";
4) int maxSpeed=120;
5) Vehicle(String type,int maxSpeed)
6) {
7) this.type=type;
8) this.maxSpeed=maxSpeed;
9) }
10) }
11) class Car extends Vehicle
12) {
13) String trans;
14) Car(String trans)
15) {
16) //Line-1
17) this.trans=trans;
18) }
19) Car(String type,it maxSpeed,String trans)
20) {
21) super(type,maxSpeed);
22) this(trans);//Line-2
23) }
24) }
A. 4w 120 Auto
4w 150 Manual
B. null 0 Auto
4w 150 Manual
72 https://www.youtube.com/durgasoftware
OCJA
C. Compilatio Fails only at Line-1
D. Compilatio Fails only at Line-2
E. Compilatio Fails at both Line-1 and Line-2
Answer: E
Q2. Given:
1) class CD
2) {
3) int r;
4) CD(int r)
5) {
6) this.r=r;
7) }
8) }
9) class DVD extends CD
10) {
11) int c;
12) DVD(int r, int c)
13) {
14) //line-1
15) }
16) }
Which code fragment should be inserted at Line-1 to instantiate dvd object successfully?
A) super.r=r;
this.c=c;
B) super(r);
this(c);
C) super(r);
this.c=c;
D) this.c=r;
super(c)
Answer: C
73 https://www.youtube.com/durgasoftware
OCJA
Q3. Given the code Fragment:
74 https://www.youtube.com/durgasoftware
OCJA
Answer: A and C
Q4. Given:
1) class A
2) {
3) public A()
4) {
5) System.out.println("A");
6) }
7) }
8) class B extends A
9) {
10) public B()
11) {
12) //line-1
13) System.out.println("B");
14) }
15) }
16) class C extends B
17) {
18) public C()
19) {
20) //line-2
21) System.out.println("C");
22) }
23) public static void main(String[] args)
24) {
25) C c = new C();
26) }
27) }
A) CBA
B) C
C) ABC
D) Compilation Fails at line-1 and line-2
Answer: C
Q5. Given
1) class Vehicle
2) {
3) int x;
4) Vehicle()
5) {
75 https://www.youtube.com/durgasoftware
OCJA
6) this(10);// line-1
7) }
8) Vehicle(int x)
9) {
10) this.x=x;
11) }
12) }
13) class Car extends Vehicle
14) {
15) int y;
16) Car()
17) {
18) super();
19) this(20);//line-2
20) }
21) Car(int y)
22) {
23) this.y= y;
24) }
25) public String toString()
26) {
27) return super.x+":"+this.y;
28) }
29) }
A. 10:20
A. 0:20
C. Compilation Fails at Line-1
D. Compilation Fails at Line-2
Answer: D
76 https://www.youtube.com/durgasoftware
OCJA
8) setName(name);
9) }
10) public Person(String name,int age)
11) {
12) Person(name);//Line-2
13) setAge(age);
14) }
15) //setter and getter methods go here
16) public String show()
17) {
18) return name+" "+age+" "+number;
19) }
20) public static void main(String[] args)
21) {
22) Person p1= new Person("Durga");
23) Person p2= new Person("Ravi",50);
24) System.out.println(p1.show());
25) System.out.println(p2.show());
26) }
27) }
Answer: D
Q7. Given:
1) class Animal
2) {
3) String type="Canine";
4) int maxSpeed=60;
5) Animal(){}
6) Animal(String type,int maxSpeed)
7) {
8) this.type=type;
9) this.maxSpeed=maxSpeed;
10) }
11) }
12) class WildAnimal extends Animal
13) {
14) String bounds;
15) WildAnimal(String bounds)
77 https://www.youtube.com/durgasoftware
OCJA
16) {
17) //line-1
18) }
19) WildAnimal(String type,int maxSpeed,String bounds)
20) {
21) //line-2
22) }
23) }
Canine 60 Long
Feline 80 Short
Answer: A and E
Q8.
1) class Employee
2) {
3) private String name;
78 https://www.youtube.com/durgasoftware
OCJA
4) private int age;
5) private int salary;
6)
7) public Employee(String name,int age)
8) {
9) setName(name);
10) setAge(age);
11) setSalary(2000);
12) }
13) public Employee(String name,int age,int salary)
14) {
15) setSalary(salary);
16) this(name,age);
17) }
18) //getter and setter methods goes here
19) public void printDetails()
20) {
21) System.out.println(name+":"+age+":"+salary);
22) }
23) }
Test.java:
1) class Test
2) {
3) public static void main(String[] args)
4) {
5) Employee e1= new Employee();
6) Employee e2= new Employee("Durga",50);
7) Employee e3= new Employee("Ravi",40,5000);
8) e1.printDetails();
9) e2.printDetails();
10) e3.printDetails();
11) }
12) }
B) null:0:0
Durga:50:0
Ravi:40:5000
C) null:0:0
Durga:50:2000
Ravi:40:5000
79 https://www.youtube.com/durgasoftware
OCJA
D) Compilation Fails in the Test class
Answer: E
And the given the following main method located in another class:
A. At line-2 insert:
amount=100;
B. At line-2 insert:
this.amount=100;
C. At line-2 insert:
acct.amount=100;
D. At line-1 insert:
1) public CheckingAccount()
2) {
3) amount=100;
4) }
E. At line-1 insert:
1) public CheckingAccount()
2) {
3) this.amount=100;
4) }
80 https://www.youtube.com/durgasoftware
OCJA
F. At line-1 insert:
1) public CheckingAccount()
2) {
3) acct.amount=100;
4) }
81 https://www.youtube.com/durgasoftware
OCJA
1) class X
2) {
3) public void printFileContent()
4) {
5) //Line-1
6) throw new IOException();//Line-2
7) }
8) }
9) public class Test
10) {
11) public static void main(String[] args)//Line-3
12) {
13) X x= new X();
14) x.printFileContent();//Line-4
15) //Line-5
16) }
17) }
A. Replace Line-3 with public static void main(String[] args) throws Exception
B. Replace Line-4 with:
1) try
2) {
3) x.printFileContent();
4) }
5) catch (Exception e) {}
6) catch (IOException e) {}
C. Replace Line-3 with public static void main(String[] args) throws IOException
D. Replace Line-2 with throw IOException("Exception Raised");
E. At Line-5 insert throw new IOException();
Answer: A, C
82 https://www.youtube.com/durgasoftware
OCJA
4) {
5) System.out.println("Rearding Card");
6) }
7) void checkCard(int cno) throws RuntimeException//Line-1
8) {
9) System.out.println("Checking Card");
10) }
11) public static void main(String[] args)
12) {
13) Test t = new Test();
14) int cardNo=1234;
15) t.checkCard(cardNo);//Line-2
16) t.readCard(cardNo);//Line-3
17) }
18) }
A) Checking Card
Reading Card
Answer: D
Q3. Given the following code for the classes MyException and Test:
83 https://www.youtube.com/durgasoftware
OCJA
19) {
20) try
21) {
22) throw Math.random() > 0.5 ? new Exception():new MyException();
23) }
24) catch (RuntimeException e)
25) {
26) System.out.println("B");
27) }
28) }
29) }
Answer: E
A) element 0
element 1
B) null element 0
null element 1
C) null
null
84 https://www.youtube.com/durgasoftware
OCJA
Answer: D
A) Invalid Name
omas
null
null
B) Invalid Name
C) Invalid Name
omas
D) Compilation Fails
Answer: A
85 https://www.youtube.com/durgasoftware
OCJA
Q6. Given the code fragment:
1) import java.util.*;
2) public class Test
3) {
4) public static void main(String[] args)
5) {
6) ArrayList l = new ArrayList();
7) String[] s;
8) try
9) {
10) while(true)
11) {
12) l.add("MyString");
13) }
14) }
15) catch (RuntimeException e)
16) {
17) System.out.println("Caught a RuntimeException");
18) }
19) catch (Exception e)
20) {
21) System.out.println("Caught an Exception");
22) }
23) System.out.println("Ready to use");
24) }
25) }
Answer: C
A. Improves the program structure because the error handing code is separated from the normal
program function.
C. Improves the program structure beacuase the programmer can choose where to handle
exceptions
86 https://www.youtube.com/durgasoftware
OCJA
D. Imporves the program structure because exceptions must be handled in the method in which
they occurred.
E. Allows the creation of new exceptions that are tailored to the particular program being created.
Answer:A,C,E
Answer: B,C,E
Ans: B,E
87 https://www.youtube.com/durgasoftware
OCJA
A. Match 1
B. Match 2
C. No Match
D. NullPointerException is thrown at runtime
Answer: B
Q2. Given:
88 https://www.youtube.com/durgasoftware
OCJA
11) System.out.println(ta);
12) }
13) }
A. ABCD
B. ACD
C. ABCC
D. ABD
E. ABDC
Answer: C
A. String str2=str1;
B. String str2=new String(str1);
C. String str2=sb1.toString();
D. String str2="Durga";
Answer: A
Q4. You are developing a banking module. You have developed a class named ccMask that
has a maskcc method.
Given the code fragment:
1) class CCMask
2) {
3) public static String maskCC(String creditCard)
4) {
5) String x="XXXX-XXXX-XXXX-";
6) //Line-1
7) }
8) public static void main(String[] args)
89 https://www.youtube.com/durgasoftware
OCJA
9) {
10) System.out.println(maskCC("1234-5678-9101-1121"));
11) }
12) }
You must ensure that maskCC method returns a String that hides all digits of the credit card
number except last four digits (and the hyphens that separate each group of 4 digits)
Which two code fragments should you use at line 1, independently to achieve the
requirement?
B) return x+creditCard.substring(15,19);
Answer: B, C
A. true false
B. true true
C. false true
D. false false
Answer: D
90 https://www.youtube.com/durgasoftware
OCJA
Q6. Consider the following code:
A. String s3=s2;
if(s1==s3)
B. if(s1.equalsIgnoreCase(s2))
C. String s3=s2;
if(s1.equals(s3))
D. if(s1.toLowerCase()==s2.toLowerCase())
Answer: B
1) class MyString
2) {
3) String msg;
4) MyString(String msg)
5) {
6) this.msg=msg;
7) }
8) }
9) public class Test
10) {
11) public static void main(String[] args)
12) {
91 https://www.youtube.com/durgasoftware
OCJA
13) System.out.println("Hello "+ new StringBuilder("Java SE 8"));
14) System.out.println("Hello "+ new MyString("Java SE 8"));
15) }
16) }
A. Hello Java SE 8
Hello MyString@<hashcode>
B. Hello Java SE 8
Hello Java SE 8
C. Hello java.lang.StringBuilder@<hashcode>
Hello MyString@<hashcode>
D. Compilation Fails
Answer: A
Q8. Given:
A. 9
B. 8
C. 10
D. Compilation Fails
Answer: A
Q9. Which statement will empty the contents of a StringBuilder variable named sb?
A. sb.deleteAll();
B. sb.delete(0,sb.size());
C. sb.delete(0,sb.length());
D. sb.removeAll();
Answer: C
92 https://www.youtube.com/durgasoftware
OCJA
Q10. Given
Answer: C
93 https://www.youtube.com/durgasoftware
OCJA
1) class Test
2) {
3) public static void main(String[] args)
4) {
5) Short s1=200;
6) Integer s2=400;
7) Long s3=(long)s1+s2;//Line-1
8) String s4=(String)(s3*s2);//Line-2
9) System.out.println("Sum is:"+s4);
10) }
11) }
Answer: C
94 https://www.youtube.com/durgasoftware
OCJA
Q3. Given:
A. true..null
B. true..false
C. false..false
D. true..true
Answer: B
95 https://www.youtube.com/durgasoftware
OCJA
What is the result?
A. 123..true
123..true
B. 123..true
123..false
C. 123..false
123..true
D. Compilation Fails
Answer: A
96 https://www.youtube.com/durgasoftware
OCJA
Topic-14: ArrayList
Q1. Given the code fragment:
1) import java.util.*;
2) class Test
3) {
4) public static void main(String[] args)
5) {
6) List<String> l = new ArrayList<>();
7) l.add("Robb");
8) l.add("Bran");
9) l.add("Rick");
10) l.add("Bran");
11) if(l.remove("Bran"))
12) {
13) l.remove("Jon");
14) }
15) System.out.println(l);
16) }
17) }
Answer: A
1) import java.util.*;
2) class Test
3) {
4) public static void main(String[] args)
5) {
6) ArrayList l = new ArrayList();
7) String[] s;
8) try
9) {
10) while(true)
11) {
97 https://www.youtube.com/durgasoftware
OCJA
12) l.add("MyString");
13) }
14) }
15) catch (RuntimeException e)
16) {
17) System.out.println("RuntimeException caught");
18) }
19) catch (Exception e)
20) {
21) System.out.println("Exception caught");
22) }
23) System.out.println("Ready to use");
24) }
25) }
A. RuntimeException caught
Ready to use
B. Exception caught
Ready to use
C. Compilation Fails
Answer: D
Q3.Given:
1) import java.util.*;
2) class Patient
3) {
4) String name;
5) public Patient(String name)
6) {
7) this.name=name;
8) }
9) }
10) class Test
11) {
12) public static void main(String[] args)
13) {
14) List l = new ArrayList();
15) Patient p = new Patient("Mike");
16) l.add(p);
17) //insert code here==>Line-1
98 https://www.youtube.com/durgasoftware
OCJA
18) if(f>=0)
19) {
20) System.out.println("Mike Found");
21) }
22) }
23) }
Which code inserted at Line-1 enable the code to print Mike Found.
A.
int f=l.indexOf(p);
B.
int f=l.indexOf(Patient("Mike"));
C.
int f=l.indexOf(new Patient("Mike"));
D.
Patient p1 = new Patient("Mike");
int f=l.indexOf(p1);
Answer: A
1) import java.util.*;
2) class Test
3) {
4) public static void main(String[] args)
5) {
6) ArrayList<Integer> l = new ArrayList<>();
7) l.add(1);
8) l.add(2);
9) l.add(3);
10) l.add(4);
11) l.add(null);
12) l.remove(2);
13) l.remove(null);
14) System.out.println(l);
15) }
16) }
99 https://www.youtube.com/durgasoftware
OCJA
Q5. Given the following class declarations
A.
ArrayList<Animal> l = new ArrayList<>();
l.add(new Tiger());
B.
ArrayList<Hunter> l = new ArrayList<>();
l.add(new Cat());
C.
ArrayList<Hunter> l = new ArrayList<>();
l.add(new Tiger());
D.
ArrayList<Tiger> l = new ArrayList<>();
l.add(new Cat());
E.
ArrayList<Animal> l = new ArrayList<>();
l.add(new Cat());
Answer: D
100 https://www.youtube.com/durgasoftware
OCJA
Person.java:
Test.java:
1) class Test
2) {
3) public static void checkAge(List<Person> list,Predicate<Person> predicate)
4) {
5) for (Person p: list)
6) {
7) if(predicate.test(p))
8) {
9) System.out.println(p.name+" ");
10) }
11) }
12) }
13) public static void main(String[] args)
14) {
15) List<Person> iList=Arrays.asList(new Person("Durga",45),
16) new Person("Ravi",40),
17) new Person("Shiva",38));
18) //line-1
101 https://www.youtube.com/durgasoftware
OCJA
19) }
20) }
Which code fragment,when inserted at line-1 enables the code to print Durga?
A. checkAge(iList,()->p.getAge()>40);
B. checkAge(iList,Person p->p.getAge()>40);
C. checkAge(iList,p->p.getAge()>40);
D. checkAge(iList,(Person p)-> {p.getAge()>40;});
Answer: C
LocalDate date=LocalDate.of(yyyy,mm,dd);
only valid values we have to take for month,year and day
LocalDate dt=LocalDate.of(2012,01,32);==>invalid
LocalDate dt=LocalDate.of(2012,15,28);===>invalid
LocalDate dt=LocalDate.of(2012,7,28);===>valid
1) import java.time.*;
2) public class Test
3) {
4) public static void main(String[] args)
5) {
6) LocalDate dt=LocalDate.of(2012,01,32);
7) dt.plusDays(10);
8) System.out.println(dt);
9) }
10) }
Answer: D
102 https://www.youtube.com/durgasoftware
OCJA
RE:
Exception in thread "main" java.time.DateTimeException: Invalid value for DayOfMonth (valid
values 1 - 28/31):
32
1) import java.time.*;
2) import java.time.format.*;
3) public class Test
4) {
5) public static void main(String[] args)
6) {
7) String date=LocalDate.parse("2014-05-
04").format(DateTimeFormatter.ISO_DATE_TIME);
8) System.out.println(date);
9) }
10) }
103 https://www.youtube.com/durgasoftware
OCJA
What is the result?
A) May 04,2014T00:00:00.000
B) 2014-05-04T00:00:00.000
C) 5/4/14T00:00:00.000
D) An exception is thrown at runtime
Answer: D
RE:
Exception in thread "main" java.time.temporal.UnsupportedTemporalTypeException:
Unsupported field: HourOfDay
at java.time.LocalDate.get0(Unknown Source)
Eg:
1) LocalDateTime dt=LocalDateTime.parse("2014-05-04T13:45:45.000");
2) String s=dt.format(DateTimeFormatter.ISO_DATE_TIME);
3) System.out.println(s);
Output: 2014-05-04T13:45:45
1) import java.time.*;
2) import java.time.format.*;
3) public class Test
4) {
5) public static void main(String[] args)
6) {
7) LocalDate date1=LocalDate.now();
8) LocalDate date2=LocalDate.of(2018,4,15);
9) LocalDate date3=LocalDate.parse("2018-04-15",DateTimeFormatter.ISO_DATE);
10) System.out.println("date-1:"+date1);
11) System.out.println("date-2:"+date2);
12) System.out.println("date-3:"+date3);
13) }
14) }
104 https://www.youtube.com/durgasoftware
OCJA
What is the result?
A.
date-1:2018-04-15
date-2:2018-04-15
date-3:2018-04-15
B.
date-1:04/15/2018
date-2:2018-04-15
date-3:Apr 15,2018
C. Compilation Fails
Answer: A
1) import java.time.*;
2) import java.time.format.*;
3) public class Test
4) {
5) public static void main(String[] args)
6) {
7) LocalDateTime dt=LocalDateTime.of(2014,7,31,1,1);
8) dt.plusDays(30);
9) dt.plusMonths(1);
10) System.out.println(dt.format(DateTimeFormatter.ISO_DATE));
11) }
12) }
A. 2014-07-31
B. 07-31-2014
C. 2014-09-30
D. An Exception is thrown at runtime
Answer: A
With these new objects will be created and dt is always point to specified date only(2014,7,31,1,1)
105 https://www.youtube.com/durgasoftware
OCJA
A. 1
B. 2
C. 3
D. 4
Answer: A
1) class Student
2) {
3) String name;
4) int age;
5) }
6) And,
7) public class Test
8) {
9) public static void main(String[] args)
10) {
11) Student s1= new Student();
12) Student s2= new Student();
106 https://www.youtube.com/durgasoftware
OCJA
13) Student s3= new Student();
14) s1=s3;
15) s3=s2;
16) s2=null;-->line-1
17) }
18) }
Answer: C
107 https://www.youtube.com/durgasoftware