0% found this document useful (0 votes)
60 views17 pages

Oca Java02

Uploaded by

jikeyov429
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
60 views17 pages

Oca Java02

Uploaded by

jikeyov429
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

3.

QUESTION

OCA JAVA02 Which of the following is not a valid array


declaration?

A- int[] arr1 = new int[8]


1. QUESTION B- int arr1[][] = new int[][8]
C- int[][] arr1 = new int[8][8]
What will be the result of compiling and executing
D- int[] arr1 []= new int[8][]
Test class?

1. package com.udayan.oca;
2. 4. QUESTION
3. public class Test {
4. char c; Fill in the blanks for the definition of
5. double d;
6. float f;
java.lang.Error class:
7. public static void main(String[] args) {
8. Test obj = new Test();
public class java.lang.Error extends ________________ {...}
9. System.out.println(">" + obj.c);
10. System.out.println(">" + obj.d);
11. System.out.println(">" + obj.f);
12. } A- RuntimeException
Throwable
13. }
B-
C- Exception
A- >null
>0.0
>0.0 5. QUESTION
B- > What will be the result of compiling and executing
>0.0 Test class?
>0.0f
C- > 1. package com.udayan.oca;
2.
>0.0 3. public class Test {
>0.0 4. public static void main(String[] args) {
5. String fruit = "mango";
D- >null 6. switch (fruit) {
>0.0 7. default:
8. System.out.println("ANY FRUIT WILL DO");
>0.0f 9. case "Apple":
10. System.out.println("APPLE");
11. case "Mango":
12. System.out.println("MANGO");
2. QUESTION 13. case "Banana":
14. System.out.println("BANANA");
For the class Test, which options, if used to 15. break;
replace /*INSERT*/, will print "Hurrah! I 16. }
17. }
passed..." on to the console? 18. }

1. public class Test {


2. /*INSERT*/ {
3. System.out.println("Hurrah! I passed...");
4. }
5. } A- MANGO
B- ANY FRUIT WILL DO
C- ANY FRUIT WILL DO
A- public void static main(String[] args) APPLE
B- static public void main(String[] args) MANGO
C- public void main(String[] args) BANANA
D- protected static void main(String[] args)
D- MANGO
E- static public void Main(String[] args)
BANANA

6. QUESTION
What will be the result of compiling and executing
Test class?
1. package com.udayan.oca; 2.
2. 3. public class Test {
3. public class Test { 4. public static void main(String[] args) {
4. public static void main(String[] args) { 5. /*INSERT*/
5. String str = "java"; 6. switch(var) {
6. StringBuilder sb = new StringBuilder("java"); 7. case 7:
7. 8. System.out.println("Lucky no. 7");
8. System.out.println(str.equals(sb) + ":" + sb.equals(str)); 9. break;
9. } 10. default:
10. } 11. System.out.println("DEFAULT");
12. }
13. }
14. }
A- true:false
B- false:false A- char var = ‘7’;
C- true:true B- char var = 7;
D- false:true C- Character var ‘7’;
E- Compilation error D- Character var = 7;
E- Integer var 7;

7. QUESTION
Consider below code: 9. QUESTION
For the given code snippet:
1. //Test.java
2. package com.udayan.oca; List<String> list = new /*INSERT*/();
3.
4. import java.time.LocalDate; Which of the following options, if used to replace
5.
6. class MyLocalDate extends LocalDate { /*INSERT*/, compiles successfully?
7. @Override
8. public String toString() { A- List<>
9. return super.getDayOfMonth() + "-" + super.getMonthValue()
+
B- ArrayList<>
10. "-" + super.getYear(); C- ArrayList<String>
11. } D- List<String>
12. }
13.
14. public class Test {
15. public static void main(String [] args) {
16. MyLocalDate date = LocalDate.parse("1980-03-16"); 10. QUESTION
17. System.out.println(date); What will be the result of compiling and executing
18. }
19. } Test class?

1. package com.udayan.oca;
What will be the result of compiling and executing 2.
Test class? 3. public class Test {
4. private static void m(int x) {
5. System.out.println("int version");
A- Compilation error 6. }
B- 16-03-1980 7.
C- 16-3-1980 8. private static void m(char x) {
9. System.out.println("char version");
D- 1980-03-16 10. }
E- An exception is thrown at runtime 11.
12. public static void main(String [] args) {
13. int i = '5';
14. m(i);
15. m('5');
16. }
17. }

A- char version
int version
8. QUESTION B- char version
For the class Test, which options, if used to char version
replace /*INSERT*/, will print "Lucky no. 7" on C- Compilation error
to the console? Select 3 options. D- int version
char version
1. package com.udayan.oca;

2
E- int version Which of the following is a checked Exception?
int version
A- FileNotFoundException
B- RuntimeException
11. QUESTION C- ExceptionInInitializerError
D- ClassCastException
What will be the result of compiling and executing
Test class?

1. package com.udayan.oca; 14. QUESTION


2.
3. public class Test {
What will be the result of compiling and executing
4. public static void main(String[] args) { Test class?
5. try {
6. main(args);
1. package com.udayan.oca;
7. } catch (Exception ex) {
2.
8. System.out.println("CATCH-");
3. public class Test {
9. }
4. public static void main(String[] args) {
10. System.out.println("OUT");
5. String str1 = new String("Core");
11. }
6. String str2 = new String("CoRe");
12. }
7. System.out.println(str1 = str2);
8. }
A- OUT 9. }
B- None of the System.out.println statements
are executed A- true
C- Compilation error B- false
D- CATCH-OUT C- Core
D- CoRe

12. QUESTION
Consider the code of Test.java file: 15. QUESTION
What will be the result of compiling and executing
1. package com.udayan.oca; Test class?
2.
3. class Student {
1. package com.udayan.oca;
4. String name;
2.
5. int age;
3. import java.io.FileNotFoundException;
6.
4. import java.io.IOException;
7. void Student() {
5.
8. Student("James", 25);
6. abstract class Super {
9. }
7. public abstract void m1() throws IOException;
10.
8. }
11. void Student(String name, int age) {
9.
12. this.name = name;
10. class Sub extends Super {
13. this.age = age;
11. @Override
14. }
12. public void m1() throws IOException {
15. }
13. throw new FileNotFoundException();
16.
14. }
17. public class Test {
15. }
18. public static void main(String[] args) {
16.
19. Student s = new Student();
17. public class Test {
20. System.out.println(s.name + ":" + s.age);
18. public static void main(String[] args) {
21. }
19. Super s = new Sub();
22. }
20. try {
21. s.m1();
What will be the result of compiling and executing 22. } catch (FileNotFoundException e) {
23. System.out.print("M");
Test class? 24. } finally {
25. System.out.print("N");
A- James:25 26. }
27. }
B- null:0 28. }
C- An exception is thrown at runtime
D- Compilation error A- N
B- Compilation error
C- Program ends abruptly
13. QUESTION D- MN

3
16. QUESTION 1.
2.
//A.java
package com.udayan.oca;
What will be the result of compiling and executing 3.
4. public class A {
Test class? 5. public void print() {
6. System.out.println("A");
1. package com.udayan.oca; 7. }
2. 8. }
3. public class Test { 9.
4. public static void main(String[] args) { 10. //B.java
5. System.out.println("Output is: " + 10 != 5); 11. package com.udayan.oca;
6. } 12.
7. } 13. public class B extends A {
14. public void print() {
15. System.out.println("B");
A- Output is: false 16. }
B- Compilation error 17. }
C- Output is: true 18.
19. //Test.java
D- Output is: 10 != 5 20. package com.udayan.oca.test;
21.
22. import com.udayan.oca.*;
23.
17. QUESTION 24. public class Test {
25. public static void main(String[] args) {
What will be the result of compiling and executing 26. A obj1 = new A();
Test class? 27. B obj2 = (B)obj1;
28. obj2.print();
29. }
1. package com.udayan.oca; 30. }
2.
3. import java.util.function.Predicate;
4. What will be the result of compiling and executing
5. public class Test {
6. public static void main(String[] args) {
Test class?
7. String [] arr = {"A", "ab", "bab", "Aa", "bb", "baba", "aba", "A
bab"}; A- ClassCastException is thrown at runtime
8.
9. Predicate<String> p = s -> s.toUpperCase().substring(0,1).equals(
B- A
"A"); C- B
10. D- Compilation error
11. processStringArray(arr, p);
12. }
13.
14. private static void processStringArray(String [] arr, Predicate<Stri
ng> predicate) {
15. for(String str : arr) {
16. if(predicate.test(str)) {
17. System.out.println(str);
18. }
19. }
20. }
21. }

A- Runtime exception 19. QUESTION


B- ab
aba Consider code of Test.java file:
C- A
1. package com.udayan.oca;
Aa 2. import java.util.ArrayList;
Abab 3. import java.util.List;
D- A 4.
5. public class Test {
ab 6. public static void main(String[] args) {
Aa 7. List<Character> list = new ArrayList<>();
aba 8. list.add(0, 'V');
Abab 9. list.add('T');
10. list.add(1, 'E');
E- Compilation error 11. list.add(3, 'O');
12.
13. if(list.contains('O')) {
14. list.remove('O');
18. QUESTION 15. }
16.
Consider codes below: 17. for(char ch : list) {
18. System.out.print(ch);

4
19. } Which of the following code correctly extracts
20. }
21. } country code from the swift code referred by
String reference variable swiftCode?
What will be the result of compiling and executing
Test class? A- swiftCode.substring(5, 7);
B- swiftCode.substring(4, 6);
A- VTEO
C- swiftCode.substring(5, 6);
B- VTE
C- VET D- swiftCode.substring(4, 5);
D- Compilation error
E- Runtime exception
F- VETO
22. QUESTION
What will be the result of compiling and executing
20. QUESTION Test class?
Given the code of Test.java file: 1. //Test.java
2. package com.udayan.oca;
1. package com.udayan.oca; 3.
2. 4. class Student {
3. class Point { 5. String name;
4. int x; 6. int marks;
5. int y; 7.
6. void assign(int x, int y) { 8. Student(String name, int marks) {
7. x = this.x; 9. this.name = name;
8. this.y = y; 10. this.marks = marks;
9. } 11. }
10. 12. }
11. public String toString() { 13. public class Test {
12. return "Point(" + x + ", " + y + ")"; 14. public static void main(String[] args) {
13. } 15. Student student = new Student("James", 25);
14. } 16. int marks = 25;
15. 17. review(student, marks);
16. public class Test { 18. System.out.println(marks + "-" + student.marks);
17. public static void main(String[] args) { 19. }
18. Point p1 = new Point(); 20.
19. p1.x = 10; 21. private static void review(Student stud, int marks) {
20. p1.y = 20; 22. marks = marks + 10;
21. Point p2 = new Point(); 23. stud.marks+=marks;
22. p2.assign(p1.x, p1.y); 24. }
23. System.out.println(p1.toString() + ";" + p2.toString()); 25. }
24. }
25. }
A- 35-25
B- 35-60
What will be the result of compiling and executing C- 25-25
Test class? D- 25-60

A- Point(10, 20);Point(0, 20)


B- Point(0, 20);Point(0, 20)
23. QUESTION
C- Point(0, 20);Point(10, 20)
D- None of the other options Consider below code:
E- Point(10, 20);Point(10, 20)
1. public class Test {
2. static {
3. System.out.println(1/0);
21. QUESTION 4.
5.
}

A bank's swift code is generally of 11 characters 6. public static void main(String[] args) {
7. System.out.println("HELLO");
and used in international money transfers. 8. }
An example of swift code: ICICINBBRT4 9. }
ICIC: First 4 letters for bank code
IN: Next 2 letters for Country code On execution, does Test class print "HELLO" on
BB: Next 2 letters for Location code to the console?
RT4: Next 3 letters for Branch code
A- Yes, HELLO is printed on to the console
5
B- No, HELLO is not printed on the console 9. package orders.items;
10.
11. public class Item {
12.
13. }
24. QUESTION 14.
15. //Shop.java
Consider below code: 16. package shopping;
17.
1. //Test.java 18. /*INSERT*/
2. package com.udayan.oca; 19.
3. 20. public class Shop {
4. import java.time.LocalDate; 21. Order order = null;
5. import java.time.LocalTime; 22. Item item = null;
6. 23. }
7. public class Test {
8. public static void main(String [] args) {
9. LocalDate date = LocalDate.parse("1947-08-14"); For the class Shop, which options, if used to
10. LocalTime time = LocalTime.MAX; replace /*INSERT*/, will resolve all the
11. System.out.println(date.atTime(time));
12. } compilation errors? Select 2 options.
13. }

A- import orders.items.*;
What will be the result of compiling and executing B-
Test class? 1. import orders.*;
2. import orders.items.*;

A- 1947-08-14T23:59:59.0 C-
1. import orders.Order;
B- 1947-08-14T23:59:59 2. import orders.items.Item;
import orders.*;
C- 1947-08-14T23:59:59.999 D-
D- 1947-08-14T23:59:59.999999999
E-
1. import orders.*;
2. import items.*;

25. QUESTION
Consider below code:

1. //Test.java
2. package com.udayan.oca; 27. QUESTION
3.
4. public class Test { Consider below code:
5. public static void main(String[] args) {
6. StringBuilder sb = new StringBuilder(100);
1. //Test.java
7. System.out.println(sb.length() + ":" + sb.toString().length());
2. package com.udayan.oca;
8. }
3.
9. }
4. public class Test {
5. public static void main(String[] args) {
What will be the result of compiling and executing 6. String s1 = "OCAJP";
7. String s2 = "OCAJP" + "";
Test class? 8. System.out.println(s1 == s2);
9. }
10. }
A- 0:0
B- 16:16
C- 100:100 What will be the result of compiling and executing
D- 16:0 Test class?
E- 100:0
A- false
B- OCAJP
26. QUESTION C- Compilation error
Consider 3 files:

1.
2.
//Order.java
package orders;
28. QUESTION
3. Consider below code:
4. public class Order {
5.
6. } 1. //Test.java
7. 2. package com.udayan.oca;
8. //Item.java 3.

6
4. import java.util.ArrayList; D- Happy Holidays!
5.
6. class Counter {
Happy Holidays!
7. int count;
8. Counter(int count) {
9. this.count = count;
10. } 30. QUESTION
11.
12. public String toString() { For the class Test, which options, if used to
13. return "Counter-" + count; replace /*INSERT*/, will print [5, 10] on to the
14. }
15. } console? Select 3 options.
16.
17. public class Test { 1. package com.udayan.oca;
18. public static void main(String[] args) { 2.
19. ArrayList<Counter> original = new ArrayList<>(); 3. public class Test {
20. original.add(new Counter(10)); 4. public static void main(String[] args) {
21. 5. /*INSERT*/
22. ArrayList<Counter> cloned = (ArrayList<Counter>) original.clo 6. arr[0] = 5;
ne(); 7. arr[1] = 10;
23. cloned.get(0).count = 5; 8. System.out.println("[" + arr[0] + ", " + arr[1] + "]");
24. 9. }
25. System.out.println(original); 10. }
26. }
27. }
A- short [] arr = {};
What will be the result of compiling and executing B- short [2] arr;
Test class? C- short [] arr = new short[2]{100, 100};
D- short [] arr = new short[]{100, 100};
A- An exception is thrown at runtime
B- [Counter-10]
C- Compilation error
D- [Counter-5]
E-
1. short [] arr;
2. arr = new short[2];
29. QUESTION F- short arr [] = new short[2];
What will be the result of compiling and executing
Test class?

1. package com.udayan.oca;
31. QUESTION
2. What will be the result of compiling and executing
3. class Message {
4. String msg = "Happy New Year!"; Test class?
5.
6. public void print() { 1. package com.udayan.oca;
7. System.out.println(msg); 2.
8. } 3. public class Test {
9. } 4. public static void main(String[] args) {
10. 5. Double [] arr = new Double[2];
11. public class Test { 6. System.out.println(arr[0] + arr[1]);
12. public static void change(Message m) { 7. }
13. m = new Message(); 8. }
14. m.msg = "Happy Holidays!";
15. }
16. A- 0.0
17. public static void main(String[] args) { B- Compilation error
18. Message obj = new Message(); C- NullPointerException is thrown at runtime
19. obj.print();
20. change(obj); D- ClassCastException is thrown at runtime
21. obj.print();
22. }
23. }
32. QUESTION
A- Happy New Year! Which of the following correctly defines class
Happy New Year! Printer?
B- Happy New Year!
Happy Holidays! A-
C- null
Happy New Year! 1. import java.util.*;
7
2.
3.
package com.udayan.oca;
public class Printer {
34. QUESTION
4. Consider below code:
5. }
1. //Test.java
B- 2. package com.udayan.oca;
3.
4. import java.time.LocalDate;
6. public class Printer {
5.
7. package com.udayan.oca;
6. public class Test {
8. }
7. public static void main(String [] args) {
8. LocalDate newYear = LocalDate.of(2018, 1, 1);
C- 9. LocalDate christmas = LocalDate.of(2018, 12, 25);
10. boolean flag1 = newYear.isAfter(christmas);
11. boolean flag2 = newYear.isBefore(christmas);
9. public class Printer { 12. System.out.println(flag1 + ":" + flag2);
10. 13. }
11. } 14. }
12. package com.udayan.oca;

D-

13. /* Java Developer Comments. */ What will be the result of compiling and executing
14. package com.udayan.oca; Test class?
15. public class Printer {
16.
17. } A- Compilation error
B- An exception is thrown at runtime
C- false:true
D- true:false
33. QUESTION
What will be the result of compiling and executing
Test class? 35. QUESTION
1. package com.udayan.oca;
____________ uses access modifiers to protect
2. variables and hide them within a class.
3. public class Test { Which of the following options accurately fill in
4. public static void main(String[] args) {
5. int x = 1; the blanks above?
6. while(checkAndIncrement(x)) {
7. System.out.println(x);
8. }
A- Encapsulation
9. } B- Abstraction
10. C- Polymorphism
11. private static boolean checkAndIncrement(int x) { D- Inheritance
12. if(x < 5) {
13. x++;
14. return true;
15. } else {
16. return false; 36. QUESTION
17. }
18. }
What will be the result of compiling and executing
19. } Test class?

A- 1 1. package com.udayan.oca;
2.
2 3. import java.time.LocalTime;
3 4.
4 5. public class Test {
6. public static void main(String[] args) {
B- Infinite loop
7. LocalTime time = LocalTime.of(16, 40);
C- 2 8. String amPm = time.getHour() >= 12 ? (time.getHour() == 12) ? "
3 PM" : "AM";
4 9. System.out.println(amPm);
10. }
5 11. }
D- 1
2 A- PM
3 B- AM
4 C- Compilation error
5 D- An exception is thrown at runtime
8
37. QUESTION 5.
6.
switch(var) {
case 100:
How many objects of Pen class are eligible for 7. System.out.println("var is 100");
8. break;
Garbage Collection at Line 4? 9. case 200:
10. System.out.println("var is 200");
1. package com.udayan.oca; 11. break;
2. 12. default:
3. class Pen { 13. System.out.println("In default");
4. 14. }
5. } 15. }
6. 16. }
7. public class TestPen {
8. public static void main(String[] args) {
9. new Pen(); //Line 1 A- var is 100
10. Pen p = new Pen(); // Line 2 B- In default
11. change(p); //Line 3 C- Compilation error
12. System.out.println("About to end."); //Line 4
13. } D- var is 200
14.
15. public static void change(Pen pen) { //Line 5
16. pen = new Pen(); //Line 6
17. } 40. QUESTION
18. }
Consider below code:
A- 0 1. //Test.java
B- 1 2. package com.udayan.oca;
C- 3 3.
D- 2 4. import java.time.LocalDateTime;
5.
6. public class Test {
7. public static void main(String [] args) {
8. LocalDateTime obj = LocalDateTime.now();
38. QUESTION 9. System.out.println(obj.getSecond());
10. }
What will be the result of compiling and executing 11. }
Test class?
Which of the following statement is correct?
1. import java.util.ArrayList;
2. import java.util.List;
3. A- Code fails to compile
4. public class Test {
B- It will print any int value between 1 and 60
5. public static void main(String[] args) {
6. List<Integer> list = new ArrayList<>(); C- Code compiles successfully but throws
7. list.add(100); Runtime exception
8. list.add(200); D- It will print any int value between 0 and 59
9. list.add(100);
10. list.add(200);
11. list.remove(100);
12.
13. System.out.println(list); 41. QUESTION
14. }
15. }
Which of these access modifiers can be used for a
top level interface?
A- [100, 200, 200]
A- All of the other options
B- [200]
B- protected
C- [200, 100, 200] C- public
D- Exception is thrown at runtime D- private
E- [200, 200]

42. QUESTION
39. QUESTION What will be the result of compiling and executing
What will be the result of compiling and executing the Test class?
Test class?
1. package com.udayan.oca;
2.
1. package com.udayan.oca; 3. public class Test {
2. public class Test { 4. public static void main(String[] args) {
3. public static void main(String[] args) { 5. int grade = 60;
4. byte var = 100; 6. if(grade = 60)

9
7. System.out.println("You passed...");
8. else
9. System.out.println("You failed...");
10. }
11. }

A- Compilation error
B- You passed…
C- Produces no output
D- You failed…

43. QUESTION 45. QUESTION


Consider below code fragment: Consider below code:
1. interface Printable { 1. //Test.java
2. public void setMargin(); 2. package com.udayan.oca;
3. public void setOrientation(); 3.
4. } 4. import java.time.Period;
5. 5.
6. abstract class Paper implements Printable {//Line7 6. public class Test {
7. public void setMargin() {} 7. public static void main(String [] args) {
8. //Line 9 8. Period period = Period.of(0, 0, 0);
9. } 9. System.out.println(period);
10. 10. }
11. class NewsPaper extends Paper { //Line 12 11. }
12. public void setMargin() {}
13. //Line 14
14. } What will be the result of compiling and executing
Test class?
Above code is currently giving compilation error.
Which 2 modifications, done independently, A- p0d
enable the code to compile? B- P0D
C- P0Y0M0D
D- p0y0m0d
A- Replace the code at Line 7 with: class Paper
implements Printable {
B- Insert at Line 14: public void setOrientation() {} 46. QUESTION
C- Replace the code at Line 12 with: abstract Consider below code:
class NewsPaper extends Paper {
1. //Test.java
D- Insert at Line 9: public abstract void 2. package com.udayan.oca;
3.
setOrientation(); 4. import java.time.LocalDate;
5.
6. public class Test {
7. public static void main(String [] args) {
8. LocalDate date = LocalDate.of(2020, 9, 31);
44. QUESTION 9. System.out.println(date);
What will be the result of compiling and executing 10. }
11. }
Test class?

1. package com.udayan.oca;
What will be the result of compiling and executing
2. Test class?
3. public class Test {
4. public static void main(String[] args) {
5. double [] arr = new int[2]; //Line 3 A- An exception is thrown at runtime
6. System.out.println(arr[0]); //Line 4 B- Compilation error
7. } C- 2020-10-01
8. }
D- 2020-09-30
A- 0.0
B- Line 3 causes compilation error
C- 0 47. QUESTION
D- Line 4 causes runtime exception

10
What will be the result of compiling and executing 15. }
Test class?
What will be the result of compiling and executing
1. //Test.java Test class?
2. package com.udayan.oca.test;
3.
4. abstract class Animal { A- 01-11-11
5. private String name; B- 11-11-11
6.
7. Animal(String name) {
C- 11-11-12
8. this.name = name; D- Runtime exception
9. } E- 01-11-12
10.
11. public String getName() {
12. return name;
13. }
14. } 49. QUESTION
15. Consider below code:
16. class Dog extends Animal {
17. private String breed;
18. Dog(String breed) { 1. //Test.java
19. this.breed = breed; 2. package com.udayan.oca;
20. } 3.
21. 4. class SpecialString {
22. Dog(String name, String breed) { 5. String str;
23. super(name); 6. SpecialString(String str) {
24. this.breed = breed; 7. this.str = str;
25. } 8. }
26. 9. }
27. public String getBreed() { 10.
28. return breed; 11. public class Test {
29. } 12. public static void main(String[] args) {
30. } 13. System.out.println(new String("Java"));
31. 14. System.out.println(new StringBuilder("Java"));
32. public class Test { 15. System.out.println(new SpecialString("Java"));
33. public static void main(String[] args) { 16. }
34. Dog dog1 = new Dog("Beagle"); 17. }
35. Dog dog2 = new Dog("Bubbly", "Poodle");
36. System.out.println(dog1.getName() + ":" + dog1.getBreed() + ":" + d
og2.getName() + ":" + dog2.getBreed()); What will be the result of compiling and executing
37. } Test class?
38. }

A- Java
A- Compilation error for Animal Class Java
B- null:Beagle:Bubbly:Poodle Java
C- Compilation error for Dog(String) constructor B- Java
D- :Beagle:Bubbly:Poodle Java
E- Compilation error for Dog(String, String) <Some text containing @ symbol>
constructor C- Compilation error
F- Compilation error for Animal(String) D- Java
constructor <Some text containing @ symbol>
<Some text containing @ symbol>

48. QUESTION
Consider below code: 50. QUESTION
Consider below code:
1. //Test.java
2. package com.udayan.oca;
3. 1. //Test.java
4. import java.time.LocalDate; 2. package com.udayan.oca;
5. import java.time.Period; 3. import java.util.ArrayList;
6. import java.time.format.DateTimeFormatter; 4. import java.util.List;
7. 5.
8. public class Test { 6. public class Test {
9. public static void main(String [] args) { 7. public static void main(String[] args) {
10. LocalDate date = LocalDate.of(2012, 1, 11); 8. String s = new String("Hello");
11. Period period = Period.ofMonths(2); 9. List<String> list = new ArrayList<>();
12. DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM- 10. list.add(s);
dd-yy"); 11. list.add(new String("Hello"));
13. System.out.print(formatter.format(date.minus(period))); 12. list.add(s);
14. } 13. s.replace("l", "L");

11
14.
15. System.out.println(list);
16. }
17. }

53. QUESTION
What will be the result of compiling and executing
What will be the result of compiling and executing
Test class?
Test class?
A- [HeLLo, Hello, HeLLo] 1. package com.udayan.oca;
B- [HeLLo, Hello, Hello] 2.
C- [HeLLo, HeLLo, HeLLo] 3. public class Test {
4. public static void main(String[] args) {
D- [Hello, Hello, Hello] 5. short [] args = new short[]{50, 50};
6. args[0] = 5;
7. args[1] = 10;
8. System.out.println("[" + args[0] + ", " + args[1] + "]");
51. QUESTION 9. }
10. }
What will be the result of compiling and executing
Test class? A- An exception is thrown at runtime
B- [5, 10]
1. package com.udayan.oca;
2. C- Compilation error
3. public class Test { D- [50, 50]
4. public static void main(String[] args) {
5. String [] fruits = {"apple", "banana", "mango", "orange"};
6. for(String fruit : fruits) {
7.
8.
System.out.print(fruit + " ");
if(fruit.equals("mango")) {
54. QUESTION
9. continue; Which of the following statement is correct about
10. }
11. System.out.println("salad!"); below code?
12. break;
13. } 1. public class Test {
14. } 2. public static void main(String[] args) {
15. } 3. do {
4. System.out.println(100);
5. } while (false);
A- apple banana orange salad! 6. System.out.println("Bye");
B- apple salad! 7. }
8. }
C- apple banana mango salad!
D- None of the other options. A- 100
Bye
B- Unreachable code compilation error
52. QUESTION C- Compiles successfully and prints 100 in
infinite loop
What will be the result of compiling and executing D- Compiles successfully and prints "Bye"
Test class?

1. package com.udayan.oca;
2. 55. QUESTION
3. public class Test {
4. public static void main(String[] args) { Below is the code of Test.java file:
5. StringBuilder sb = new StringBuilder("Java");
6. String s1 = sb.toString(); 1. import java.util.ArrayList;
7. String s2 = sb.toString(); 2. import java.util.List;
8. 3.
9. System.out.println(s1 == s2); 4. public class Test {
10. } 5. public static void main(String [] args) {
11. } 6. List<Integer> list = new ArrayList<Integer>();
7. list.add(new Integer(2));
A- Compilation error 8. list.add(new Integer(1));
9. list.add(new Integer(0));
B- false 10.
C- An exception is thrown at runtime 11. list.remove(list.indexOf(0));
D- true 12.
13. System.out.println(list);
14. }
15. }

12
What will be the result of compiling and executing A- Student[James, 27]
Test class? Student[James, 25]
Student[James, 25]
A- An exception is thrown at runtime B- Student[James, 27]
B- Compilation error C- Student[James, 25]
C- [1, 0] Student[James, 27]
D- [2, 1] Student[James, 25]
D- Student[James, 25]
Student[James, 27]
Student[James, 25]
56. QUESTION Student[James, 25]
What will be the result of compiling and executing
Test class?
58. QUESTION
1. public class Test {
2. public static void main(String [] args) { Given code:
3. int a = 100;
4. System.out.println(-a++); 1. package com.udayan.oca;
5. } 2.
6. } 3. public class Test {
4. public static void main(String[] args) {
A- -100 5. String [] arr = {"A", "B", "C", "D", "E"};
6. for(/*INSERT*/) {
B- 99 7. System.out.print(arr[n]);
C- Compilation error 8. }
D- -101 9. }
10. }
E- -99

Which option, if used to replace /*INSERT*/, on


execution will print ACE on to the console?
57. QUESTION
Consider below code: A- int n = 0; n < arr.length; n += 1

1. import java.util.ArrayList; B- int n = 1; n < arr.length; n += 2


int n = 1; n < arr.length; n += 1
2. import java.util.List;
3. C-
4. class Student { D- int n = 0; n < arr.length; n += 2
5. private String name;
6. private int age;
7.
8. Student(String name, int age) {
9. this.name = name; 59. QUESTION
10. this.age = age;
11. } What will be the result of compiling and executing
12. Test class?
13. public String toString() {
14. return "Student["+name+", " + age + "]";
15. } 1. package com.udayan.oca;
16. } 2.
17. 3. public class Test {
18. public class Test { 4. public static void main(String[] args) {
19. public static void main(String[] args) { 5. double price = 90000;
20. List<Student> students = new ArrayList<>(); 6. String model;
21. students.add(new Student("James", 25)); 7. if(price > 100000) {
22. students.add(new Student("James", 27)); 8. model = "Tesla Model X";
23. students.add(new Student("James", 25)); 9. } else if(price <= 100000) {
24. students.add(new Student("James", 25)); 10. model = "Tesla Model S";
25. 11. }
26. students.remove(new Student("James", 25)); 12. System.out.println(model);
27. 13. }
28. for(Student stud : students) { 14. }
29. System.out.println(stud);
30. }
31. }
32. }
A- Tesla Model S
B- null
What will be the result of compiling and executing
C- Compilation Error
Test class?
D- Tesla Model X
13
60. QUESTION 28.
29.
public double getSalary() {
return salary;
Consider codes below: 30. }
31.
32. public String toString() {
1. //A.java 33. return name;
2. package com.udayan.oca; 34. }
3. 35. }
4. public class A { 36.
5. public int i1 = 1; 37. public class Test {
6. protected int i2 = 2; 38. public static void main(String [] args) {
7. } 39. List<Employee> list = new ArrayList<>();
8. 40. list.add(new Employee("James", 25, 15000));
9. //B.java 41. list.add(new Employee("Lucy", 23, 12000));
10. package com.udayan.oca.test; 42. list.add(new Employee("Bill", 27, 10000));
11. 43. list.add(new Employee("Jack", 19, 5000));
12. import com.udayan.oca.A; 44. list.add(new Employee("Liya", 20, 8000));
13. 45.
14. public class B extends A { 46. process(list, /*INSERT*/);
15. public void print() { 47.
16. A obj = new A(); 48. System.out.println(list);
17. System.out.println(obj.i1); //Line 8 49. }
18. System.out.println(obj.i2); //Line 9 50.
19. System.out.println(this.i2); //Line 10 51. private static void process(List<Employee> list, Predicate<Employee> p
20. System.out.println(super.i2); //Line 11 redicate) {
21. } 52. Iterator<Employee> iterator = list.iterator();
22. 53. while(iterator.hasNext()) {
23. public static void main(String [] args) { 54. if(predicate.test(iterator.next()))
24. new B().print(); 55. iterator.remove();
25. } 56. }
26. } 57. }
58. }
One of the statements inside print() method is
causing compilation failure. Which of the below Which of the following lambda expressions, if
solutions will help to resolve compilation error? used to replace /*INSERT*/, prints [Jack, Liya] on
to the console?
A- Comment the statement at Line 10
B- Comment the statement at Line 9 A- e -> { e.getSalary() >= 10000 }
C- Comment the statement at Line 8
B- e -> e.getSalary() >= 10000
D- Comment the statement at Line 11
C- e - > e.getSalary() >= 10000
D- (e) -> { e.getSalary() >= 10000; }
61. QUESTION E- (Employee e) -> { return e.getSalary() >= 10000; }
Consider below code:

1. //Test.java
2. package com.udayan.oca;
3.
4. import java.util.ArrayList;
5. import java.util.Iterator;
6. import java.util.List;
7. import java.util.function.Predicate;
8.
9. class Employee {
10. private String name;
11. private int age;
12. private double salary;
13. 62. QUESTION
14. public Employee(String name, int age, double salary) {
15. this.name = name; Consider below code:
16. this.age = age;
17. this.salary = salary; 1. import java.util.ArrayList;
18. } 2. import java.util.List;
19. 3.
20. public String getName() { 4. public class Test {
21. return name; 5. public static void main(String[] args) {
22. } 6. List<String> list1 = new ArrayList<>();
23. 7. list1.add("A");
24. public int getAge() { 8. list1.add("D");
25. return age; 9.
26. } 10. List<String> list2 = new ArrayList<>();
27. 11. list2.add("B");
14
12. list2.add("C"); Consider the following class:
13.
14. list1.addAll(1, list2);
15. 1. public class Employee {
16. System.out.println(list1); 2. public int passportNo; //line 2
17. } 3. }
18. }

Which of the following is the correct way to make


What will be the result of compiling and executing the variable 'passportNo' read only for any other
Test class? class?

A- [A, B, C] A- Make 'passportNo' static and provide a public


B- [A, D, B, C] static method getPassportNo() which will
C- [A, B, C, D] return its value.
D- [A, D] B- Make 'passportNo' private.
C- Make 'passportNo' private and provide a public
method getPassportNo() which will return its
63. QUESTION value.
D- Remove 'public' from the 'passportNo'
Consider code below: declaration. i.e., change line 2 to int
passportNo;
1. package com.udayan.oca;
2.
3. class PenDrive {
4. int capacity;
5. PenDrive(int capacity) { 65. QUESTION
6. this.capacity = capacity;
7. }
Consider below code:
8. }
9. 1. import java.util.ArrayList;
10. class OTG extends PenDrive { 2. import java.util.Iterator;
11. String type; 3. import java.util.List;
12. OTG(int capacity, String type) { 4.
13. //TODO 5. public class Test {
14. } 6. public static void main(String[] args) {
15. } 7. List<String> dryFruits = new ArrayList<>();
16. 8. dryFruits.add("Walnut");
17. public class Test { 9. dryFruits.add("Apricot");
18. public static void main(String[] args) { 10. dryFruits.add("Almond");
19. OTG obj = new OTG(64, "TYPE-C"); 11. dryFruits.add("Date");
20. System.out.println(obj.capacity + ":" + obj.type); 12.
21. } 13. Iterator<String> iterator = dryFruits.iterator();
22. } 14. while(iterator.hasNext()) {
23. 15. String dryFruit = iterator.next();
16. if(dryFruit.startsWith("A")) {
17. dryFruits.remove(dryFruit);
Currently above code is giving compilation error. 18. }
Which of the options can successfully replace 19. }
20.
//TODO statement such that on executing Test 21. System.out.println(dryFruits);
class, output is 64:TYPE-C? 22. }}

A- What will be the result of compiling and executing


Test class?
1. this.type = type;
2. super(capacity);
A- [Walnut, Date]
B- [Walnut, Apricot, Almond, Date]
B- super(capacity); C- Compilation error
C- D- An exception is thrown at runtime
a. super.capacity = capacity;
b. this.type = type;
D-
a. super(capacity); 66. QUESTION
b. this.type = type;
What will be the result of compiling and executing
Test class?
64. QUESTION 1. public class Test {
2. public static void main(String[] args) {
15
3. int [] arr = {2, 1, 0}; 16. dates.removeIf(x -> x.getYear() < 2000);
4. for(int i : arr) { 17.
5. System.out.println(arr[i]); 18. System.out.println(dates);
6. } 19. }
7. } 20. }
8. }

A- [1919-02-25, 1980-12-31]
A- Compilation error B- [2018-07-11, 2020-04-08]
B- ArrayIndexOutOfBoundsException is thrown C- Runtime exception
at runtime D- [2018-07-11, 1919-02-25, 2020-04-08, 1980-
C- 0 12-31]
1
2
D- 2
1 69. QUESTION
0 What will be the result of compiling and executing
Test class?

1. public class Test {


67. QUESTION 2. public static void main(String[] args) {
What will be the result of compiling and executing 3. m1(); //Line 3
4. }
Test class? 5.
6. private static void m1() throws Exception { //Line 6
1. public class Test { 7. System.out.println("NOT THROWING ANY EXCEPTION"); //Line 7
2. public static void main(String[] args) { 8. }
3. StringBuilder sb = new StringBuilder(); 9. }
4. System.out.println(sb.append(null).length());
5. }
6. } A- Compilation error at Line 3
B- Compilation error at Line 7
A- 4 C- NOT THROWING ANY EXCEPTION
B- 1 D- Compilation error at Line 6
C- Compilation error
D- NullPointerException is thrown at runtime

70. QUESTION
Consider below code:

68. QUESTION 1. //Guest.java


2. class Message {
What will be the result of compiling and executing 3. static void main(String [] args) {
Test class? 4. System.out.println("Welcome! " + args[1]);
5. }
6. }
1. package com.udayan.oca;
7.
2.
8. public class Guest {
3. import java.time.LocalDate;
9. public static void main(String [] args) {
4. import java.time.Month;
10. Message.main(args);
5. import java.util.ArrayList;
11. }
6. import java.util.List;
12. }
7.
8. public class Test {
9. public static void main(String[] args) { And the commands:
10. List<LocalDate> dates = new ArrayList<>();
11. dates.add(LocalDate.parse("2018-07-11")); javac Guest.java
12. dates.add(LocalDate.parse("1919-02-25")); java Guest James Gosling
13. dates.add(LocalDate.of(2020, 4, 8));
14. dates.add(LocalDate.of(1980, Month.DECEMBER, 31)); What is the result?
15.
16
A- ArrayIndexOutOfBoundsException is thrown
at runtime.
B- Some other error as main method can't be
invoked manually.
C- Compilation error as main method is not
public.
D- Welcome! James
E- Welcome! Gosling

17

You might also like