Java SE 8 Programmer I – OCA (1Z0-808) Total Questions: 775 – 13 Mock Exams & 1 Master Cheat Sheet
Practice Set 1
Time limit: 0
0 of 60 questions completed
Questions:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Information
Click on Start Quiz.
You have already completed the Test before. Hence you can not start it again.
Test is loading...
You must sign in or sign up to start the Test.
You have to finish following quiz, to start this Test:
Your results are here!! for" Java SE 8 Programmer I - OCA (1Z0-808) Practice Test 1 "
0 of 60 questions answered correctly
Your time:
Time has elapsed
Your Final Score is : 0
You have attempted : 0
Number of Correct Questions : 0 and scored 0
Number of Incorrect Questions : 0 and Negative marks 0
Average score
Your score
Java SE 8 Programmer I - OCA (1Z0-808)
You have attempted: 0
Number of Correct Questions: 0 and scored 0
Number of Incorrect Questions: 0 and Negative marks 0
You can review your answers by clicking view questions. Important Note : Open Reference Documentation Links in New Tab (Right Click and Open in New Tab).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Answered
Review
Question 1 of 60
1. Question
Which of the following correctly defines class Printer?
Correct
If package is used then it should be the first statement, but javadoc and developer comments are not considered as java statements so a class can have developer and javadoc comments before the package statement.
If import and package both are available, then correct order is package, import, class declaration.
Incorrect
If package is used then it should be the first statement, but javadoc and developer comments are not considered as java statements so a class can have developer and javadoc comments before the package statement.
If import and package both are available, then correct order is package, import, class declaration.
Unattempted
If package is used then it should be the first statement, but javadoc and developer comments are not considered as java statements so a class can have developer and javadoc comments before the package statement.
If import and package both are available, then correct order is package, import, class declaration.
Question 2 of 60
2. Question
Which of the following correctly defines class Printer?
Correct
If package is used then it should be the first statement, but javadoc and developer comments are not considered as java statements so a class can have developer and javadoc comments before the package statement.
If import and package both are available, then correct order is package, import, class declaration. Multiple package statements are not allowed.
Incorrect
If package is used then it should be the first statement, but javadoc and developer comments are not considered as java statements so a class can have developer and javadoc comments before the package statement.
If import and package both are available, then correct order is package, import, class declaration. Multiple package statements are not allowed.
Unattempted
If package is used then it should be the first statement, but javadoc and developer comments are not considered as java statements so a class can have developer and javadoc comments before the package statement.
If import and package both are available, then correct order is package, import, class declaration. Multiple package statements are not allowed.
Question 3 of 60
3. Question
For the code below, what should be the name of java file?
package com.skillcertpro.oca;
public class HelloWorld {
public static void main(String [] args) {
System.out.println(“Hello World!”);
}
}
Correct
Java is case sensitive language. File name should match with public class’s name, which is “HelloWorld”.
“helloworld” is different from “HelloWorld”.
Incorrect
Java is case sensitive language. File name should match with public class’s name, which is “HelloWorld”.
“helloworld” is different from “HelloWorld”.
Unattempted
Java is case sensitive language. File name should match with public class’s name, which is “HelloWorld”.
“helloworld” is different from “HelloWorld”.
Question 4 of 60
4. Question
Does below code compile successfully?
package com.skillcertpro.oca;
public class Test {
public static void main(String [] args) {
System.out.println(“Hello”);;;;;;;;;
}
}
Correct
In java, it is allowed to put multiple statements on one line. E.g. below code is legal:
public class Test {
public static void main(String [] args) {
String symbol = “!”;System.out.print(“Hello “);System.out.print(“World”);System.out.println(symbol);
}
}
Above code is similar to:
public class Test {
public static void main(String [] args) {
String symbol = “!”;
System.out.print(“Hello “);
System.out.print(“World”);
System.out.println(symbol);
}
}
Empty statements (just the semicolon) are also allowed in java, therefore below code is also legal:
public class Test {
public static void main(String [] args) {
System.out.println(“Hello”);
;
;
;
;
;
;
;
;
}
}
As shown above, java statements (including empty statements) can be placed on one line, therefore below code is legal:
public class Test {
public static void main(String [] args) {
System.out.println(“Hello”);;;;;;;;;
}
}
Incorrect
In java, it is allowed to put multiple statements on one line. E.g. below code is legal:
public class Test {
public static void main(String [] args) {
String symbol = “!”;System.out.print(“Hello “);System.out.print(“World”);System.out.println(symbol);
}
}
Above code is similar to:
public class Test {
public static void main(String [] args) {
String symbol = “!”;
System.out.print(“Hello “);
System.out.print(“World”);
System.out.println(symbol);
}
}
Empty statements (just the semicolon) are also allowed in java, therefore below code is also legal:
public class Test {
public static void main(String [] args) {
System.out.println(“Hello”);
;
;
;
;
;
;
;
;
}
}
As shown above, java statements (including empty statements) can be placed on one line, therefore below code is legal:
public class Test {
public static void main(String [] args) {
System.out.println(“Hello”);;;;;;;;;
}
}
Unattempted
In java, it is allowed to put multiple statements on one line. E.g. below code is legal:
public class Test {
public static void main(String [] args) {
String symbol = “!”;System.out.print(“Hello “);System.out.print(“World”);System.out.println(symbol);
}
}
Above code is similar to:
public class Test {
public static void main(String [] args) {
String symbol = “!”;
System.out.print(“Hello “);
System.out.print(“World”);
System.out.println(symbol);
}
}
Empty statements (just the semicolon) are also allowed in java, therefore below code is also legal:
public class Test {
public static void main(String [] args) {
System.out.println(“Hello”);
;
;
;
;
;
;
;
;
}
}
As shown above, java statements (including empty statements) can be placed on one line, therefore below code is legal:
public class Test {
public static void main(String [] args) {
System.out.println(“Hello”);;;;;;;;;
}
}
Question 5 of 60
5. Question
What is the signature of special main method?
Correct
Special main method should have public access specifier and it takes argument of String [] type.
String [] argument can use any identifier name, even though in most of the cases you will see “args” is used.
Incorrect
Special main method should have public access specifier and it takes argument of String [] type.
String [] argument can use any identifier name, even though in most of the cases you will see “args” is used.
Unattempted
Special main method should have public access specifier and it takes argument of String [] type.
String [] argument can use any identifier name, even though in most of the cases you will see “args” is used.
Question 6 of 60
6. Question
Which of the following correctly imports Animal class from com.masaimara package?
Correct
Following import statements are correct:Â
import com.masaimara.*;Â
import com.masaimara.Animal;Â
NOTE: all small case letters in import keyword.
Incorrect
Following import statements are correct:Â
import com.masaimara.*;Â
import com.masaimara.Animal;Â
NOTE: all small case letters in import keyword.
Unattempted
Following import statements are correct:Â
import com.masaimara.*;Â
import com.masaimara.Animal;Â
NOTE: all small case letters in import keyword.
Question 7 of 60
7. Question
Which of the following is the correct package declaration to declare Test class in com.exam.oca package?
Correct
To declare Test class in com.exam.oca package, use following declaration:Â
package com.exam.oca;Â
No wildcard (*) allowed in package declaration. Don’t include class name in package declaration. NOTE: all small case letters in package keyword.
Incorrect
To declare Test class in com.exam.oca package, use following declaration:Â
package com.exam.oca;Â
No wildcard (*) allowed in package declaration. Don’t include class name in package declaration. NOTE: all small case letters in package keyword.
Unattempted
To declare Test class in com.exam.oca package, use following declaration:Â
package com.exam.oca;Â
No wildcard (*) allowed in package declaration. Don’t include class name in package declaration. NOTE: all small case letters in package keyword.
Question 8 of 60
8. Question
Consider following code snippet:
package com.skillcertpro.test;
public class Exam {
public static void main(String [] args) {
System.out.println(“All the best!”);
}
}
Location of Exam.java file:
D:.
????WORK
????QUIZ
????SEC07
????classes
????src
????com
????skillcertpro
????test
Exam.java
You are currently at Sec07 folder.
D:\WORK\Quiz\Sec07>
Which of the following javac command, typed from above location, will generate Exam.class file structure under classes directory?
D:.
????WORK
????QUIZ
????SEC07
????classes
? ????com
? ????skillcertpro
? ????test
? Exam.class
?
????src
????com
????skillcertpro
????test
Exam.java
Correct
Use -d option with javac command.
As you are typing javac command from within Sec07 directory, hence path of java file relative to Sec07 directory needs to be given.
So, correct command is: javac -d classes\ src\com\skillcertpro\test\Exam.java
Incorrect
Use -d option with javac command.
As you are typing javac command from within Sec07 directory, hence path of java file relative to Sec07 directory needs to be given.
So, correct command is: javac -d classes\ src\com\skillcertpro\test\Exam.java
Unattempted
Use -d option with javac command.
As you are typing javac command from within Sec07 directory, hence path of java file relative to Sec07 directory needs to be given.
So, correct command is: javac -d classes\ src\com\skillcertpro\test\Exam.java
Question 9 of 60
9. Question
Consider following code snippet:
package com.skillcertpro.test;
public class Exam {
public static void main(String [] args) {
System.out.println(“All the best!”);
}
}
Location of files:
D:.
????WORK
????QUIZ
????SEC07
????classes
? ????com
? ????skillcertpro
? ????test
? Exam.class
?
????src
????com
????skillcertpro
????test
Exam.java
You are currently at WORK folder.
D:\WORK>
Which of the following java command will show All the best! on to the console?
Correct
To execute Exam class from WORK folder, you should specify the classpath (Quiz\Sec07\classes\) which contains whole path of the class(com\skillcertpro\test\Exam.class).
And you should also use fully qualified name of the class, which is com.skillcertpro.test.Exam.
Hence correct option is: java -cp Quiz\Sec07\classes\ com.skillcertpro.test.Exam
Incorrect
To execute Exam class from WORK folder, you should specify the classpath (Quiz\Sec07\classes\) which contains whole path of the class(com\skillcertpro\test\Exam.class).
And you should also use fully qualified name of the class, which is com.skillcertpro.test.Exam.
Hence correct option is: java -cp Quiz\Sec07\classes\ com.skillcertpro.test.Exam
Unattempted
To execute Exam class from WORK folder, you should specify the classpath (Quiz\Sec07\classes\) which contains whole path of the class(com\skillcertpro\test\Exam.class).
And you should also use fully qualified name of the class, which is com.skillcertpro.test.Exam.
Hence correct option is: java -cp Quiz\Sec07\classes\ com.skillcertpro.test.Exam
Question 10 of 60
10. Question
What will be the result of compiling and executing Test class?
java Test good morning everyone
private class Test
{
public static void main(String args[])
{
System.out.println(args[1]);
}
}
Correct
Top level class can have two access modifiers: public and default.
Over here Test class has private modifier and hence compilation error.
Incorrect
Top level class can have two access modifiers: public and default.
Over here Test class has private modifier and hence compilation error.
Unattempted
Top level class can have two access modifiers: public and default.
Over here Test class has private modifier and hence compilation error.
public class Guest {
public static void main(String [] args) {
Message.main(args);
}
}
And the commands:
javac Guest.java
java Guest Clare Waight Keller
What is the result?
Correct
Class Guest has special main method but main method defined in Message class is not public and hence it can’t be called by JVM. But there is no issue with the syntax hence no compilation error.
java Guest Clare Waight Keller passes new String [] {“Clare”, “Waight”, “Keller”} to args of Guest.main method.
Guest.main method invokes Message.main method with the same argument: new String [] {“Clare”, “Waight”, “Keller”}. args[2] is “Keller” hence “Welcome Keller!” gets printed on to the console.
Incorrect
Class Guest has special main method but main method defined in Message class is not public and hence it can’t be called by JVM. But there is no issue with the syntax hence no compilation error.
java Guest Clare Waight Keller passes new String [] {“Clare”, “Waight”, “Keller”} to args of Guest.main method.
Guest.main method invokes Message.main method with the same argument: new String [] {“Clare”, “Waight”, “Keller”}. args[2] is “Keller” hence “Welcome Keller!” gets printed on to the console.
Unattempted
Class Guest has special main method but main method defined in Message class is not public and hence it can’t be called by JVM. But there is no issue with the syntax hence no compilation error.
java Guest Clare Waight Keller passes new String [] {“Clare”, “Waight”, “Keller”} to args of Guest.main method.
Guest.main method invokes Message.main method with the same argument: new String [] {“Clare”, “Waight”, “Keller”}. args[2] is “Keller” hence “Welcome Keller!” gets printed on to the console.
public class Guest {
public static void main(String [] args) {
Message.main(args);
}
}
And the commands:
javac Guest.java
java Guest James Gosling
What is the result?
Correct
Both the classes contain special main method. No compilation error with the code: file is correctly names as Guest.java (name of public class).
java Guest James Gosling passes new String [] {“James”, “Gosling”} to args of Guest.main method. Apart from being special main method, Message.main is static method so Guest.main method invokes Message.main method with the same argument: new String [] {“James”, “Gosling”}.
args[0] is “James” hence “Welcome James!” gets printed on to the console.
Incorrect
Both the classes contain special main method. No compilation error with the code: file is correctly names as Guest.java (name of public class).
java Guest James Gosling passes new String [] {“James”, “Gosling”} to args of Guest.main method. Apart from being special main method, Message.main is static method so Guest.main method invokes Message.main method with the same argument: new String [] {“James”, “Gosling”}.
args[0] is “James” hence “Welcome James!” gets printed on to the console.
Unattempted
Both the classes contain special main method. No compilation error with the code: file is correctly names as Guest.java (name of public class).
java Guest James Gosling passes new String [] {“James”, “Gosling”} to args of Guest.main method. Apart from being special main method, Message.main is static method so Guest.main method invokes Message.main method with the same argument: new String [] {“James”, “Gosling”}.
args[0] is “James” hence “Welcome James!” gets printed on to the console.
Question 13 of 60
13. Question
For the class Test, which options, if used to replace /*INSERT*/, will print “Hurrah! I passed…” on to the console? Select ALL that apply.
package com.skillcertpro.oca;
public class Test {
/*INSERT*/ {
System.out.println(“Hurrah! I passed…”);
}
}
Correct
As System.out.println needs to be executed on executing the Test class, this means special main method should replace /*INSERT*/.
Special main method’s name should be “main” (all characters in lower case), should be static, should have public access specifier and it accepts argument of String [] type.
String [] argument can use any identifier name, even though in most of the cases you will see “args” is used.
Position of static and public can be changed but return type must come just before the method name.
Incorrect
As System.out.println needs to be executed on executing the Test class, this means special main method should replace /*INSERT*/.
Special main method’s name should be “main” (all characters in lower case), should be static, should have public access specifier and it accepts argument of String [] type.
String [] argument can use any identifier name, even though in most of the cases you will see “args” is used.
Position of static and public can be changed but return type must come just before the method name.
Unattempted
As System.out.println needs to be executed on executing the Test class, this means special main method should replace /*INSERT*/.
Special main method’s name should be “main” (all characters in lower case), should be static, should have public access specifier and it accepts argument of String [] type.
String [] argument can use any identifier name, even though in most of the cases you will see “args” is used.
Position of static and public can be changed but return type must come just before the method name.
Question 14 of 60
14. Question
Suppose you have created a java file, “MyClass.java”.Â
Which of the following commands will compile the java file?
Correct
Command to compile a java file: javac .java [.java extension is compulsory].Â
Command to execute a java class: java [.class extension should not be used].
Incorrect
Command to compile a java file: javac .java [.java extension is compulsory].Â
Command to execute a java class: java [.class extension should not be used].
Unattempted
Command to compile a java file: javac .java [.java extension is compulsory].Â
Command to execute a java class: java [.class extension should not be used].
Question 15 of 60
15. Question
Consider 3 files:
//Order.java
package orders;
public class Order {
}
//Item.java
package orders.items;
public class Item {
}
//Shop.java
package shopping;
/*INSERT*/
public class Shop {
Order order = null;
Item item = null;
}
For the class Shop, which options, if used to replace /*INSERT*/, will resolve all the compilation errors? Select 2 options.
Correct
If you check the directory structure, you will find that directory “orders” contains “items”, but orders and orders.items are different packages.
import orders.*; will only import all the classes in orders package but not in orders.items package.
You need to import Order and Item classes.
To import Order class, use either import orders.Order; OR import orders.*; and to import Item class, use either import orders.items.Item; OR import orders.items.*;
Incorrect
If you check the directory structure, you will find that directory “orders” contains “items”, but orders and orders.items are different packages.
import orders.*; will only import all the classes in orders package but not in orders.items package.
You need to import Order and Item classes.
To import Order class, use either import orders.Order; OR import orders.*; and to import Item class, use either import orders.items.Item; OR import orders.items.*;
Unattempted
If you check the directory structure, you will find that directory “orders” contains “items”, but orders and orders.items are different packages.
import orders.*; will only import all the classes in orders package but not in orders.items package.
You need to import Order and Item classes.
To import Order class, use either import orders.Order; OR import orders.*; and to import Item class, use either import orders.items.Item; OR import orders.items.*;
public class A {
public int i1;
protected int i2;
int i3;
private int i4;
}
//TestA.java
package com.skillcertpro.oca.test;
import com.skillcertpro.oca.A; //Line 3
public class TestA {
public static void main(String[] args) {
A obj = new A(); //Line 7
System.out.println(obj.i1); //Line 8
System.out.println(obj.i2); //Line 9
System.out.println(obj.i3); //Line 10
System.out.println(obj.i4); //Line 11
}
}
Which of the following 3 statements are true?
Correct
class A is declared public and defined in com.skillcertpro.oca package, there is no problem in accessing class A outside com.skillcertpro.oca package.
class TestA is defined in com.skillcertpro.oca.test package, to use class A either use import statement “import com.skillcertpro.oca.A;” or fully qualified name of the class com.skillcertpro.oca.A. No issues at Line 3 and LIne 7.
As TestA is in different package so it can only access public members of class A using object reference. Line 8 compiles successfully.
protected, default and private members are not accessible outside com.skillcertpro.oca package using object reference.
NOTE: protected members can be accessed outside but only through inheritance and not object reference.
Incorrect
class A is declared public and defined in com.skillcertpro.oca package, there is no problem in accessing class A outside com.skillcertpro.oca package.
class TestA is defined in com.skillcertpro.oca.test package, to use class A either use import statement “import com.skillcertpro.oca.A;” or fully qualified name of the class com.skillcertpro.oca.A. No issues at Line 3 and LIne 7.
As TestA is in different package so it can only access public members of class A using object reference. Line 8 compiles successfully.
protected, default and private members are not accessible outside com.skillcertpro.oca package using object reference.
NOTE: protected members can be accessed outside but only through inheritance and not object reference.
Unattempted
class A is declared public and defined in com.skillcertpro.oca package, there is no problem in accessing class A outside com.skillcertpro.oca package.
class TestA is defined in com.skillcertpro.oca.test package, to use class A either use import statement “import com.skillcertpro.oca.A;” or fully qualified name of the class com.skillcertpro.oca.A. No issues at Line 3 and LIne 7.
As TestA is in different package so it can only access public members of class A using object reference. Line 8 compiles successfully.
protected, default and private members are not accessible outside com.skillcertpro.oca package using object reference.
NOTE: protected members can be accessed outside but only through inheritance and not object reference.
Question 17 of 60
17. Question
Consider below code:Â
public class Test {
public static void main(String[] args) {
System.out.println(“ONE”);
}
public static void main(Integer[] args) {
System.out.println(“TWO”);
}
public static void main(byte [] args) {
System.out.println(“THREE”);
}
}
What will be the result if Test class is executed by below command?
java Test 10
Correct
Like any other method, main method can also be overloaded. But main method called by JVM is always with String [] parameter.
Don’t get confused with 10 as it is passed as “10”. Run above class with any command line arguments or 0 command line argument, output will always be ONE.
Incorrect
Like any other method, main method can also be overloaded. But main method called by JVM is always with String [] parameter.
Don’t get confused with 10 as it is passed as “10”. Run above class with any command line arguments or 0 command line argument, output will always be ONE.
Unattempted
Like any other method, main method can also be overloaded. But main method called by JVM is always with String [] parameter.
Don’t get confused with 10 as it is passed as “10”. Run above class with any command line arguments or 0 command line argument, output will always be ONE.
Question 18 of 60
18. Question
Below is the code of Test.java file.
package com.skillcertprokhattry.oca;
public class Test {
/* INSERT */
}
Below are the definitions of main method:
1
public static final void main(String… a) {
  System.out.println(“Java Rocks!”);
}
2
public void main(String… args) {
  System.out.println(“Java Rocks!”);
}
3
static void main(String [] args) {
  System.out.println(“Java Rocks!”);
}
4
public static void main(String [] args) {
  System.out.println(“Java Rocks!”);
}
5
public static void main(String args) {
  System.out.println(“Java Rocks!”);
}
How many definitions of main method can replace /* INSERT */ such that on executing Test class, “Java Rocks!” is printed on to the console?
Correct
Special main method (called by JVM on execution) should be static and should have public access modifier. It also takes argument of String [] type (Varargs syntax String… can also be used).
String [] or String… argument can use any identifier name, even though in most of the cases you will see “args” is used.
final modifier can be used with this special main method.
Hence, from the given five definitions of main method, below two definitions will print expected output on to the console.
public static final void main(String… a) {
  System.out.println(“Java Rocks!”);
}
and
public static void main(String [] args) {
  System.out.println(“Java Rocks!”);
}
Incorrect
Special main method (called by JVM on execution) should be static and should have public access modifier. It also takes argument of String [] type (Varargs syntax String… can also be used).
String [] or String… argument can use any identifier name, even though in most of the cases you will see “args” is used.
final modifier can be used with this special main method.
Hence, from the given five definitions of main method, below two definitions will print expected output on to the console.
public static final void main(String… a) {
  System.out.println(“Java Rocks!”);
}
and
public static void main(String [] args) {
  System.out.println(“Java Rocks!”);
}
Unattempted
Special main method (called by JVM on execution) should be static and should have public access modifier. It also takes argument of String [] type (Varargs syntax String… can also be used).
String [] or String… argument can use any identifier name, even though in most of the cases you will see “args” is used.
final modifier can be used with this special main method.
Hence, from the given five definitions of main method, below two definitions will print expected output on to the console.
public static final void main(String… a) {
  System.out.println(“Java Rocks!”);
}
and
public static void main(String [] args) {
  System.out.println(“Java Rocks!”);
}
Question 19 of 60
19. Question
Given code of Thought.java file:
public class Thought {
/*INSERT*/ {
System.out.println(“All is well”);
}
}
Which 3 options, if used to replace /*INSERT*/, will compile successfully and on execution will print “All is well” on to the console?
Correct
As System.out.println needs to be executed on executing the Test class, this means special main method should replace /*INSERT*/.
Special main method’s name should be “main” (all characters in lower case), should be static, should have public access specifier and it accepts argument of String [] type (Varargs syntax String… can also be used). String [] argument can use any identifier name, even though in most of the cases you will see “args” is used. Position of static and public can be changed but return type ‘void’ must come just before the method name.
Let’s check all the given options one by one:
public void static main(String [] args): Compilation error as return type ‘void’ must come just before the method name ‘main’.
protected static void main(String [] args): Compiles successfully but as this method is not public, hence an Error regarding missing main method is thrown on execution.
public void main(String… args): Compiles successfully but as this method is not static, hence an Error regarding non-static main method is thrown on execution.
static public void Main(String [] args): Compiles successfully but as ‘M’ is capital in method ‘Main’, hence it is not special main method. An Error regarding missing main method is thrown on execution.
static public void main(String [] args): Valid definition, it compiles successfully and on execution prints “All is well” on to the console.
public static void main(String [] a): Valid definition, it compiles successfully and on execution prints “All is well” on to the console.
public static Void main(String [] args): Compilation error as Void is a final class in Java and in this case compiler expects main method to return a value of Void type. If you add `return null;` to the main method code will compile successfully but on execution an Error will be thrown mentioning that return type must be ‘void’ (‘v’ in lower-case).
public static void main(String… a): Valid definition, it compiles successfully and on execution prints “All is well” on to the console.
Incorrect
As System.out.println needs to be executed on executing the Test class, this means special main method should replace /*INSERT*/.
Special main method’s name should be “main” (all characters in lower case), should be static, should have public access specifier and it accepts argument of String [] type (Varargs syntax String… can also be used). String [] argument can use any identifier name, even though in most of the cases you will see “args” is used. Position of static and public can be changed but return type ‘void’ must come just before the method name.
Let’s check all the given options one by one:
public void static main(String [] args): Compilation error as return type ‘void’ must come just before the method name ‘main’.
protected static void main(String [] args): Compiles successfully but as this method is not public, hence an Error regarding missing main method is thrown on execution.
public void main(String… args): Compiles successfully but as this method is not static, hence an Error regarding non-static main method is thrown on execution.
static public void Main(String [] args): Compiles successfully but as ‘M’ is capital in method ‘Main’, hence it is not special main method. An Error regarding missing main method is thrown on execution.
static public void main(String [] args): Valid definition, it compiles successfully and on execution prints “All is well” on to the console.
public static void main(String [] a): Valid definition, it compiles successfully and on execution prints “All is well” on to the console.
public static Void main(String [] args): Compilation error as Void is a final class in Java and in this case compiler expects main method to return a value of Void type. If you add `return null;` to the main method code will compile successfully but on execution an Error will be thrown mentioning that return type must be ‘void’ (‘v’ in lower-case).
public static void main(String… a): Valid definition, it compiles successfully and on execution prints “All is well” on to the console.
Unattempted
As System.out.println needs to be executed on executing the Test class, this means special main method should replace /*INSERT*/.
Special main method’s name should be “main” (all characters in lower case), should be static, should have public access specifier and it accepts argument of String [] type (Varargs syntax String… can also be used). String [] argument can use any identifier name, even though in most of the cases you will see “args” is used. Position of static and public can be changed but return type ‘void’ must come just before the method name.
Let’s check all the given options one by one:
public void static main(String [] args): Compilation error as return type ‘void’ must come just before the method name ‘main’.
protected static void main(String [] args): Compiles successfully but as this method is not public, hence an Error regarding missing main method is thrown on execution.
public void main(String… args): Compiles successfully but as this method is not static, hence an Error regarding non-static main method is thrown on execution.
static public void Main(String [] args): Compiles successfully but as ‘M’ is capital in method ‘Main’, hence it is not special main method. An Error regarding missing main method is thrown on execution.
static public void main(String [] args): Valid definition, it compiles successfully and on execution prints “All is well” on to the console.
public static void main(String [] a): Valid definition, it compiles successfully and on execution prints “All is well” on to the console.
public static Void main(String [] args): Compilation error as Void is a final class in Java and in this case compiler expects main method to return a value of Void type. If you add `return null;` to the main method code will compile successfully but on execution an Error will be thrown mentioning that return type must be ‘void’ (‘v’ in lower-case).
public static void main(String… a): Valid definition, it compiles successfully and on execution prints “All is well” on to the console.
Question 20 of 60
20. Question
Consider below code of main.java file:
package main;
public class main {
static String main = “ONE”;
public main() {
System.out.println(“TWO”);
}
public static void main(String [] args) {
main();
}
public static void main() {
System.out.println(main);
}
}
Also consider below statements:
1. Code doesn’t compile
2. Code compiles successfully
3. Only ONE will be printed to the console
4. Only TWO will be printed to the console
5. Both ONE and TWO will be printed to the console
How many of the above statements is/are true?
Correct
Though given code looks strange but it is possible in java to provide same name to package, class (and constructor), variable and method.
Above code compiles successfully and on execution prints ONE on to the console. Constructor is not invoked as ‘new’ keyword is not used and that is why TWO will not be printed to the console.
In real world coding, you would not see such code and that is why it is a good question for the certification exam.
Incorrect
Though given code looks strange but it is possible in java to provide same name to package, class (and constructor), variable and method.
Above code compiles successfully and on execution prints ONE on to the console. Constructor is not invoked as ‘new’ keyword is not used and that is why TWO will not be printed to the console.
In real world coding, you would not see such code and that is why it is a good question for the certification exam.
Unattempted
Though given code looks strange but it is possible in java to provide same name to package, class (and constructor), variable and method.
Above code compiles successfully and on execution prints ONE on to the console. Constructor is not invoked as ‘new’ keyword is not used and that is why TWO will not be printed to the console.
In real world coding, you would not see such code and that is why it is a good question for the certification exam.
Question 21 of 60
21. Question
Consider incomplete code of M.java file
class M {
}
________ class N {
}
Following options are available to fill the above blank:
1. public
2. private
3. protected
4. final
5. abstract
How many above options can be used to fill above blank (separately and not together) such that there is no compilation error?
Correct
Top-level class can use only two access modifiers [public and default(don’t specify anything)]. private and protected cannot be used.
As file name is M.java, hence class N cannot be public.
Top-level class can be final, hence it is a correct option.
Top-level class can be abstract and hence it is also a correct option.
Incorrect
Top-level class can use only two access modifiers [public and default(don’t specify anything)]. private and protected cannot be used.
As file name is M.java, hence class N cannot be public.
Top-level class can be final, hence it is a correct option.
Top-level class can be abstract and hence it is also a correct option.
Unattempted
Top-level class can use only two access modifiers [public and default(don’t specify anything)]. private and protected cannot be used.
As file name is M.java, hence class N cannot be public.
Top-level class can be final, hence it is a correct option.
Top-level class can be abstract and hence it is also a correct option.
Question 22 of 60
22. Question
Given code of Test.java file:
class A {
public static void main(String [] args) {
System.out.println(“A”);
}
}
class B {
public static void main(String [] args) {
System.out.println(“B”);
}
}
class C {
public static void main(String [] args) {
System.out.println(“C”);
}
}
class D {
public static void main(String [] args) {
System.out.println(“D”);
}
}
Which of the following options is correct?
Correct
Test.java is a valid java file. As none of the classes in Test.java file are public, hence file name can use any valid Java identifier.
As file name is Test.java, hence to compile the code below command is used:
javac Test.java
Execution of above command creates 4 class files: A.class, B.class, C.class & D.class.
To print C on to the console, class C must be executed. To execute C class, command is:
java C
Incorrect
Test.java is a valid java file. As none of the classes in Test.java file are public, hence file name can use any valid Java identifier.
As file name is Test.java, hence to compile the code below command is used:
javac Test.java
Execution of above command creates 4 class files: A.class, B.class, C.class & D.class.
To print C on to the console, class C must be executed. To execute C class, command is:
java C
Unattempted
Test.java is a valid java file. As none of the classes in Test.java file are public, hence file name can use any valid Java identifier.
As file name is Test.java, hence to compile the code below command is used:
javac Test.java
Execution of above command creates 4 class files: A.class, B.class, C.class & D.class.
To print C on to the console, class C must be executed. To execute C class, command is:
java C
Question 23 of 60
23. Question
Given code of Test.java file:
public class Test {
public static void main(String[] args) {
args[1] = “Day!”;
System.out.println(args[0] + ” ” + args[1]);
}
}
And the commands:
javac Test.java
java Test Good
What is the result?
Correct
public static void main(String[] args) method is invoked by JVM.
Variable args is initialized and assigned with Program arguments. For example,
java Test: args refers to String [] of size 0.
java Test Hello: args refers to String [] of size 1 and 1st array element refers to “Hello”
java Test 1 2 3: args refers to String [] of size 3 and 1st array element refers to “1”, 2nd array element refers to “2” and 3rd array element refers to “3”.
Command used in this question: java Test Good, so args refers to String[] of size 1 and element at 0th index is “Good”.
args[1] = “Day!”; is trying to access 2nd array element at index 1, which is not available and hence an exception is thrown at runtime.
Incorrect
public static void main(String[] args) method is invoked by JVM.
Variable args is initialized and assigned with Program arguments. For example,
java Test: args refers to String [] of size 0.
java Test Hello: args refers to String [] of size 1 and 1st array element refers to “Hello”
java Test 1 2 3: args refers to String [] of size 3 and 1st array element refers to “1”, 2nd array element refers to “2” and 3rd array element refers to “3”.
Command used in this question: java Test Good, so args refers to String[] of size 1 and element at 0th index is “Good”.
args[1] = “Day!”; is trying to access 2nd array element at index 1, which is not available and hence an exception is thrown at runtime.
Unattempted
public static void main(String[] args) method is invoked by JVM.
Variable args is initialized and assigned with Program arguments. For example,
java Test: args refers to String [] of size 0.
java Test Hello: args refers to String [] of size 1 and 1st array element refers to “Hello”
java Test 1 2 3: args refers to String [] of size 3 and 1st array element refers to “1”, 2nd array element refers to “2” and 3rd array element refers to “3”.
Command used in this question: java Test Good, so args refers to String[] of size 1 and element at 0th index is “Good”.
args[1] = “Day!”; is trying to access 2nd array element at index 1, which is not available and hence an exception is thrown at runtime.
Question 24 of 60
24. Question
Consider below code of Test.java file:
public class Test {
public static void main(String [] args) {
System.out.println(“String”);
}
public static void main(Integer [] args) {
System.out.println(“Integer”);
}
public static void main(byte [] args) {
System.out.println(“byte”);
}
}
And the commands:
javac Test.java
java Test 10
What is the result?
Correct
Like any other method, main method can also be overloaded. But main method called by JVM is always with String [] parameter. Don’t get confused with 10 as it is passed as “10”.
Execute above class with any command line arguments or 0 command line argument, output will always be “String”.
Incorrect
Like any other method, main method can also be overloaded. But main method called by JVM is always with String [] parameter. Don’t get confused with 10 as it is passed as “10”.
Execute above class with any command line arguments or 0 command line argument, output will always be “String”.
Unattempted
Like any other method, main method can also be overloaded. But main method called by JVM is always with String [] parameter. Don’t get confused with 10 as it is passed as “10”.
Execute above class with any command line arguments or 0 command line argument, output will always be “String”.
Question 25 of 60
25. Question
Consider below code of Test.java file:
public class Test {
public static void main(String[] args) {
System.out.println(“Welcome ” + args[0] +”!”);
}
}
And the commands:
javac Test.java
java Test “James Gosling” “Bill Joy”
What is the result?
Correct
Please note, if passed command line arguments contain space(s) in between, then it is a common practice to enclosed within double quotes. In this case “James Gosling” is passed as one String object and “Bill Joy” is also passed as one String object.
java Test “James Gosling” “Bill Joy” passes new String [] {“James Gosling”, “Bill Joy”} to args of main method. args[0] refers to “James Gosling” and args[1] refers to “Bill Joy”.
Hence, Welcome James Gosling! is printed on to the console. While printing the String object, enclosing quotes are not shown.
To use quotes as part of the String, you can escape those using backslash, such as:
java Test “\”James Gosling”\” “\”Bill Joy”\”
Above command will print Welcome “James Gosling”! on to the console.
Incorrect
Please note, if passed command line arguments contain space(s) in between, then it is a common practice to enclosed within double quotes. In this case “James Gosling” is passed as one String object and “Bill Joy” is also passed as one String object.
java Test “James Gosling” “Bill Joy” passes new String [] {“James Gosling”, “Bill Joy”} to args of main method. args[0] refers to “James Gosling” and args[1] refers to “Bill Joy”.
Hence, Welcome James Gosling! is printed on to the console. While printing the String object, enclosing quotes are not shown.
To use quotes as part of the String, you can escape those using backslash, such as:
java Test “\”James Gosling”\” “\”Bill Joy”\”
Above command will print Welcome “James Gosling”! on to the console.
Unattempted
Please note, if passed command line arguments contain space(s) in between, then it is a common practice to enclosed within double quotes. In this case “James Gosling” is passed as one String object and “Bill Joy” is also passed as one String object.
java Test “James Gosling” “Bill Joy” passes new String [] {“James Gosling”, “Bill Joy”} to args of main method. args[0] refers to “James Gosling” and args[1] refers to “Bill Joy”.
Hence, Welcome James Gosling! is printed on to the console. While printing the String object, enclosing quotes are not shown.
To use quotes as part of the String, you can escape those using backslash, such as:
java Test “\”James Gosling”\” “\”Bill Joy”\”
Above command will print Welcome “James Gosling”! on to the console.
Question 26 of 60
26. Question
Consider codes of 3 java files:
//Planet.java
package com.skillcertprokhattry.galaxy;
public class Planet {
String name;
public Planet(String name) {
this.name = name;
}
public class TestCreator {
public static void main(String[] args) {
System.out.println(Creator.create());
}
}
And below options:
1
Add below import statement in Creator.java file:
import com.skillcertprokhattry.galaxy.Planet;
2
Add below import statement in Creator.java file:
import com.skillcertprokhattry.oca.test.TestCreator;
3
Add below import statement in TestCreator.java file:
import com.skillcertprokhattry.oca.Creator;
4
Add below import statement in TestCreator.java file:
import com.skillcertprokhattry.galaxy.Planet;
Which of the above options needs to be done so that on executing TestCreator class, “Planet: Earth” is printed on to the console?
Please note: Unnecessary imports are not allowed.
Correct
Planet is defined in ‘com.skillcertprokhattry.galaxy’ package, Creator is defined in ‘com.skillcertprokhattry.oca’ package and TestCreator is defined in ‘com.skillcertprokhattry.oca.test’ package.
Planet class doesn’t mention ‘Creator’ or ‘TestCreator’ and hence no import statements are needed in Planet class.
Creator class uses the name ‘Planet’ in its code and hence Creator class needs to import Planet class using ‘import com.skillcertprokhattry.galaxy.Planet;’ statement or ‘import com.skillcertprokhattry.galaxy.*;’ statement.
TestCreator class uses the name ‘Creator’ in its code and hence TestCreator class needs to import Creator class using ‘import com.skillcertprokhattry.oca.Creator;’ statement or ‘import com.skillcertprokhattry.oca.*;’ statement.
Please note, even though in TestCreator class, `Creator.create()` returns an instance of Planet class but as name ‘Planet’ is not used, hence Planet class is not needed to be imported.
Planet class correctly overrides toString() method, hence when an instance of Planet class is passed to println(…) method, as in the below statement:
System.out.println(Creator.create());
toString() method defined in the Planet class is invoked, which print “Planet: Earth” on to the console.
Incorrect
Planet is defined in ‘com.skillcertprokhattry.galaxy’ package, Creator is defined in ‘com.skillcertprokhattry.oca’ package and TestCreator is defined in ‘com.skillcertprokhattry.oca.test’ package.
Planet class doesn’t mention ‘Creator’ or ‘TestCreator’ and hence no import statements are needed in Planet class.
Creator class uses the name ‘Planet’ in its code and hence Creator class needs to import Planet class using ‘import com.skillcertprokhattry.galaxy.Planet;’ statement or ‘import com.skillcertprokhattry.galaxy.*;’ statement.
TestCreator class uses the name ‘Creator’ in its code and hence TestCreator class needs to import Creator class using ‘import com.skillcertprokhattry.oca.Creator;’ statement or ‘import com.skillcertprokhattry.oca.*;’ statement.
Please note, even though in TestCreator class, `Creator.create()` returns an instance of Planet class but as name ‘Planet’ is not used, hence Planet class is not needed to be imported.
Planet class correctly overrides toString() method, hence when an instance of Planet class is passed to println(…) method, as in the below statement:
System.out.println(Creator.create());
toString() method defined in the Planet class is invoked, which print “Planet: Earth” on to the console.
Unattempted
Planet is defined in ‘com.skillcertprokhattry.galaxy’ package, Creator is defined in ‘com.skillcertprokhattry.oca’ package and TestCreator is defined in ‘com.skillcertprokhattry.oca.test’ package.
Planet class doesn’t mention ‘Creator’ or ‘TestCreator’ and hence no import statements are needed in Planet class.
Creator class uses the name ‘Planet’ in its code and hence Creator class needs to import Planet class using ‘import com.skillcertprokhattry.galaxy.Planet;’ statement or ‘import com.skillcertprokhattry.galaxy.*;’ statement.
TestCreator class uses the name ‘Creator’ in its code and hence TestCreator class needs to import Creator class using ‘import com.skillcertprokhattry.oca.Creator;’ statement or ‘import com.skillcertprokhattry.oca.*;’ statement.
Please note, even though in TestCreator class, `Creator.create()` returns an instance of Planet class but as name ‘Planet’ is not used, hence Planet class is not needed to be imported.
Planet class correctly overrides toString() method, hence when an instance of Planet class is passed to println(…) method, as in the below statement:
System.out.println(Creator.create());
toString() method defined in the Planet class is invoked, which print “Planet: Earth” on to the console.
Question 27 of 60
27. Question
What will be the result of compiling and executing Test class?
package com.skillcertpro.oca;
public class Test {
public static void main(String[] args) {
byte b1 = (byte) (127 + 21);
System.out.println(b1);
}
}
Correct
127 + 21 = 148 = 00000000 00000000 00000000 10010100Â
Above binary number is positive, as left most bit is 0.Â
Same binary number after type-casting to byte: 10010100, negative number as left most bit is 1.Â
10010100 = -108.
Incorrect
127 + 21 = 148 = 00000000 00000000 00000000 10010100Â
Above binary number is positive, as left most bit is 0.Â
Same binary number after type-casting to byte: 10010100, negative number as left most bit is 1.Â
10010100 = -108.
Unattempted
127 + 21 = 148 = 00000000 00000000 00000000 10010100Â
Above binary number is positive, as left most bit is 0.Â
Same binary number after type-casting to byte: 10010100, negative number as left most bit is 1.Â
10010100 = -108.
Question 28 of 60
28. Question
What will be the result of compiling and executing TestStudent class?
//TestStudent.java
package com.skillcertpro.oca;
class Student {
String name;
int age;
boolean result;
double height;
}
public class TestStudent {
public static void main(String[] args) {
Student stud = new Student();
System.out.println(stud.name + stud.height + stud.result + stud.age);
}
}
Correct
name, height, result and age are instance variables of Student class. And instance variables are initialized to their respective default values.
name is initialized to null, age to 0, result to false and height to 0.0.
Statement System.out.println(stud.name + stud.height + stud.result + stud.age); prints null0.0false0
Incorrect
name, height, result and age are instance variables of Student class. And instance variables are initialized to their respective default values.
name is initialized to null, age to 0, result to false and height to 0.0.
Statement System.out.println(stud.name + stud.height + stud.result + stud.age); prints null0.0false0
Unattempted
name, height, result and age are instance variables of Student class. And instance variables are initialized to their respective default values.
name is initialized to null, age to 0, result to false and height to 0.0.
Statement System.out.println(stud.name + stud.height + stud.result + stud.age); prints null0.0false0
Question 29 of 60
29. Question
How can you force JVM to run Garbage Collector?
Correct
Both Runtime.getRuntime().gc(); and System.gc(); do the same thing, these make a request to JVM to run Garbage Collector.
JVM makes the best effort to run Garbage Collector but nothing is guaranteed.
Setting the reference variable to null will make the object eligible for Garbage Collection, if there are no other references to this object. But this doesn’t force JVM to run the Garbage Collector. Garbage Collection cannot be forced.
Incorrect
Both Runtime.getRuntime().gc(); and System.gc(); do the same thing, these make a request to JVM to run Garbage Collector.
JVM makes the best effort to run Garbage Collector but nothing is guaranteed.
Setting the reference variable to null will make the object eligible for Garbage Collection, if there are no other references to this object. But this doesn’t force JVM to run the Garbage Collector. Garbage Collection cannot be forced.
Unattempted
Both Runtime.getRuntime().gc(); and System.gc(); do the same thing, these make a request to JVM to run Garbage Collector.
JVM makes the best effort to run Garbage Collector but nothing is guaranteed.
Setting the reference variable to null will make the object eligible for Garbage Collection, if there are no other references to this object. But this doesn’t force JVM to run the Garbage Collector. Garbage Collection cannot be forced.
Question 30 of 60
30. Question
Given code:
package com.skillcertpro.oca;
public class Pen {
public static void main(String[] args) {
Pen p1 = new Pen(); //Line 1
Pen p2 = new Pen(); //Line 2
p1 = p2; //Line 3
p1 = null; //Line 4
}
}
When is the Pen object, created at Line 1 eligible for Garbage Collection?
Correct
At Line 3, p1 starts referring to the object referred by p2(Created at Line 2).
So, after Line 3, object created at Line 1 becomes unreachable and thus eligible for Garbage Collection.
Incorrect
At Line 3, p1 starts referring to the object referred by p2(Created at Line 2).
So, after Line 3, object created at Line 1 becomes unreachable and thus eligible for Garbage Collection.
Unattempted
At Line 3, p1 starts referring to the object referred by p2(Created at Line 2).
So, after Line 3, object created at Line 1 becomes unreachable and thus eligible for Garbage Collection.
Question 31 of 60
31. Question
How many objects of Pen class are eligible for Garbage Collection at Line 4?
package com.skillcertpro.oca;
class Pen {
}
public class TestPen {
public static void main(String[] args) {
new Pen(); //Line 1
Pen p = new Pen(); // Line 2
change(p); //Line 3
System.out.println(“About to end.”); //Line 4
}
public static void change(Pen pen) { //Line 5
pen = new Pen(); //Line 6
}
}
Correct
Object created at Line 1 becomes eligible for Garbage collection after Line 1 only, as there are no references to it. So We have one object marked for GC.Â
Object created at Line 6 becomes unreachable after change(Pen) method pops out of the STACK, and this happens after Line 3.
So at Line 4, we have two Pen objects eligible for Garbage collection: Created at Line 1 and Created at Line 6.
Incorrect
Object created at Line 1 becomes eligible for Garbage collection after Line 1 only, as there are no references to it. So We have one object marked for GC.Â
Object created at Line 6 becomes unreachable after change(Pen) method pops out of the STACK, and this happens after Line 3.
So at Line 4, we have two Pen objects eligible for Garbage collection: Created at Line 1 and Created at Line 6.
Unattempted
Object created at Line 1 becomes eligible for Garbage collection after Line 1 only, as there are no references to it. So We have one object marked for GC.Â
Object created at Line 6 becomes unreachable after change(Pen) method pops out of the STACK, and this happens after Line 3.
So at Line 4, we have two Pen objects eligible for Garbage collection: Created at Line 1 and Created at Line 6.
Question 32 of 60
32. Question
Wrapper classes are defined in which of the following package?
Correct
All the wrapper classes are defined in java.lang package.
String and StringBuilder are also defined in java.lang package and that is why import statement is not required to use these classes.
Incorrect
All the wrapper classes are defined in java.lang package.
String and StringBuilder are also defined in java.lang package and that is why import statement is not required to use these classes.
Unattempted
All the wrapper classes are defined in java.lang package.
String and StringBuilder are also defined in java.lang package and that is why import statement is not required to use these classes.
Question 33 of 60
33. Question
What will be the result of compiling and executing Test class?
package com.skillcertpro.oca;
public class Test {
char var1;
double var2;
float var3;
public static void main(String[] args) {
Test obj = new Test();
System.out.println(“>” + obj.var1);
System.out.println(“>” + obj.var2);
System.out.println(“>” + obj.var3);
}
}
Correct
Primitive type instance variables are initialized to respective zeros (byte: 0, short: 0, int: 0, long: 0L, float: 0.0f, double: 0.0, boolean: false, char: \u0000).
When printed on the console; byte, short, int & long prints 0, float & double print 0.0, boolean prints false and char prints nothing or non-printable character (whitespace).Â
Reference type instance variables are initialized to null.
Incorrect
Primitive type instance variables are initialized to respective zeros (byte: 0, short: 0, int: 0, long: 0L, float: 0.0f, double: 0.0, boolean: false, char: \u0000).
When printed on the console; byte, short, int & long prints 0, float & double print 0.0, boolean prints false and char prints nothing or non-printable character (whitespace).Â
Reference type instance variables are initialized to null.
Unattempted
Primitive type instance variables are initialized to respective zeros (byte: 0, short: 0, int: 0, long: 0L, float: 0.0f, double: 0.0, boolean: false, char: \u0000).
When printed on the console; byte, short, int & long prints 0, float & double print 0.0, boolean prints false and char prints nothing or non-printable character (whitespace).Â
Reference type instance variables are initialized to null.
Question 34 of 60
34. Question
Given the code of Test.java file:
package com.skillcertpro.oca;
class Point {
int x;
int y;
void assign(int x, int y) {
x = this.x;
this.y = y;
}
public String toString() {
return “Point(” + x + “, ” + y + “)”;
}
}
public class Test {
public static void main(String[] args) {
Point p1 = new Point();
p1.x = 10;
p1.y = 20;
Point p2 = new Point();
p2.assign(p1.x, p1.y);
System.out.println(p1.toString() + “;” + p2.toString());
}
}
What will be the result of compiling and executing Test class?
Correct
HINT: First check if members are accessible or not. All the codes are in same file Test.java, and Point class & variable x, y are declared with default modifier hence these can be accessed within the same package.
Class Test belongs to same package so no issues in accessing Point class and instance variables of Point class. Make use of pen and paper to draw the memory diagrams (heap and stack). It will be pretty quick to reach the result.
Point p1 = new Point(); means p1.x = 0 and p1.y = 0 as instance variable are initialized to respective zeros.Â
p1.x = 10; means replace 0 with 10 in p1.x,Â
p1.y = 20; means replace 0 with 20 in p1.y,Â
Point p2 = new Point(); means p2.x = 0 and p2.y = 0 as instance variable are initialized to respective zeros.Â
p2.assign(p1.x, p1.y); invokes the assign method, parameter variable x = 10 and y = 20.
As assign is invoked on p2 reference variable hence this and p2 refers to same Point object.
x = this.x; means assign 0 to parameter variable x, no changes in this.y, which means p2.x is unchanged.Â
this.y = y; means assign 20 to this.y, which means p2.y is now 20
So after assign method is invoked and control goes back to main method: p1.x = 10, p1.y = 20, p2.x = 0 and p2.y = 20.Â
Output is: Point(10, 20);Point(0, 20)
Incorrect
HINT: First check if members are accessible or not. All the codes are in same file Test.java, and Point class & variable x, y are declared with default modifier hence these can be accessed within the same package.
Class Test belongs to same package so no issues in accessing Point class and instance variables of Point class. Make use of pen and paper to draw the memory diagrams (heap and stack). It will be pretty quick to reach the result.
Point p1 = new Point(); means p1.x = 0 and p1.y = 0 as instance variable are initialized to respective zeros.Â
p1.x = 10; means replace 0 with 10 in p1.x,Â
p1.y = 20; means replace 0 with 20 in p1.y,Â
Point p2 = new Point(); means p2.x = 0 and p2.y = 0 as instance variable are initialized to respective zeros.Â
p2.assign(p1.x, p1.y); invokes the assign method, parameter variable x = 10 and y = 20.
As assign is invoked on p2 reference variable hence this and p2 refers to same Point object.
x = this.x; means assign 0 to parameter variable x, no changes in this.y, which means p2.x is unchanged.Â
this.y = y; means assign 20 to this.y, which means p2.y is now 20
So after assign method is invoked and control goes back to main method: p1.x = 10, p1.y = 20, p2.x = 0 and p2.y = 20.Â
Output is: Point(10, 20);Point(0, 20)
Unattempted
HINT: First check if members are accessible or not. All the codes are in same file Test.java, and Point class & variable x, y are declared with default modifier hence these can be accessed within the same package.
Class Test belongs to same package so no issues in accessing Point class and instance variables of Point class. Make use of pen and paper to draw the memory diagrams (heap and stack). It will be pretty quick to reach the result.
Point p1 = new Point(); means p1.x = 0 and p1.y = 0 as instance variable are initialized to respective zeros.Â
p1.x = 10; means replace 0 with 10 in p1.x,Â
p1.y = 20; means replace 0 with 20 in p1.y,Â
Point p2 = new Point(); means p2.x = 0 and p2.y = 0 as instance variable are initialized to respective zeros.Â
p2.assign(p1.x, p1.y); invokes the assign method, parameter variable x = 10 and y = 20.
As assign is invoked on p2 reference variable hence this and p2 refers to same Point object.
x = this.x; means assign 0 to parameter variable x, no changes in this.y, which means p2.x is unchanged.Â
this.y = y; means assign 20 to this.y, which means p2.y is now 20
So after assign method is invoked and control goes back to main method: p1.x = 10, p1.y = 20, p2.x = 0 and p2.y = 20.Â
Output is: Point(10, 20);Point(0, 20)
public static void main(String [] args) {
Counter c1 = new Counter();
Counter c2 = c1;
Counter c3 = null;
c2.count = 1000;
increment(c2);
}
}
On executing Counter class, how many Counter objects are created in the memory?
Correct
new Counter(); is invoked only once, hence only one Counter object is created in the memory.
c1, c2, c3 and counter are reference variables of Counter type and not Counter objects.
Incorrect
new Counter(); is invoked only once, hence only one Counter object is created in the memory.
c1, c2, c3 and counter are reference variables of Counter type and not Counter objects.
Unattempted
new Counter(); is invoked only once, hence only one Counter object is created in the memory.
c1, c2, c3 and counter are reference variables of Counter type and not Counter objects.
Question 36 of 60
36. Question
What will be the result of compiling and executing Test class?
package com.skillcertpro.oca;
public class Test {
public static void main(String[] args) {
Boolean b1 = new Boolean(“tRuE”);
Boolean b2 = new Boolean(“fAlSe”);
Boolean b3 = new Boolean(“abc”);
Boolean b4 = null;
System.out.println(b1 + “:” + b2 + “:” + b3 + “:” + b4);
}
}
Correct
Boolean class code uses equalsIgnoreCase method to validate the passed String, so if passed String is “true” (‘t’, ‘r’, ‘u’ and ‘e’ can be in any case), then boolean value stored in Boolean object is true otherwise false.
b1 stores true, b2 stores false, b3 stores false and as b4 is of reference type, hence it can store null as well.
Output is: true:false:false:null
Incorrect
Boolean class code uses equalsIgnoreCase method to validate the passed String, so if passed String is “true” (‘t’, ‘r’, ‘u’ and ‘e’ can be in any case), then boolean value stored in Boolean object is true otherwise false.
b1 stores true, b2 stores false, b3 stores false and as b4 is of reference type, hence it can store null as well.
Output is: true:false:false:null
Unattempted
Boolean class code uses equalsIgnoreCase method to validate the passed String, so if passed String is “true” (‘t’, ‘r’, ‘u’ and ‘e’ can be in any case), then boolean value stored in Boolean object is true otherwise false.
b1 stores true, b2 stores false, b3 stores false and as b4 is of reference type, hence it can store null as well.
Output is: true:false:false:null
Question 37 of 60
37. Question
What will be the result of compiling and executing Test class?
package com.skillcertpro.oca;
public static void main(String[] args) {
add(10.0, null);
}
}
Correct
add(10.0, null); => Compiler can’t convert null to double primitive type, so 2nd argument is tagged to Double reference type. So to match the method call, 10.0 is converted to Double object by auto-boxing and add(10.0, null); is tagged to add(Double, Double); method.
But at the time of execution, d2 is null so System.out.println(“Double version: ” + (d1 + d2)); throws NullPointerException.
Incorrect
add(10.0, null); => Compiler can’t convert null to double primitive type, so 2nd argument is tagged to Double reference type. So to match the method call, 10.0 is converted to Double object by auto-boxing and add(10.0, null); is tagged to add(Double, Double); method.
But at the time of execution, d2 is null so System.out.println(“Double version: ” + (d1 + d2)); throws NullPointerException.
Unattempted
add(10.0, null); => Compiler can’t convert null to double primitive type, so 2nd argument is tagged to Double reference type. So to match the method call, 10.0 is converted to Double object by auto-boxing and add(10.0, null); is tagged to add(Double, Double); method.
But at the time of execution, d2 is null so System.out.println(“Double version: ” + (d1 + d2)); throws NullPointerException.
Question 38 of 60
38. Question
What will be the result of compiling and executing Test class?
package com.skillcertpro.oca;
public class Test {
public static void main(String[] args) {
Boolean [] arr = new Boolean[2];
System.out.println(arr[0] + “:” + arr[1]);
}
}
Correct
Array elements are initialized to their default values.
arr is referring to an array of Boolean type, which is reference type and hence both the array elements are initialized to null and hence in the output null:null is printed.
Incorrect
Array elements are initialized to their default values.
arr is referring to an array of Boolean type, which is reference type and hence both the array elements are initialized to null and hence in the output null:null is printed.
Unattempted
Array elements are initialized to their default values.
arr is referring to an array of Boolean type, which is reference type and hence both the array elements are initialized to null and hence in the output null:null is printed.
Question 39 of 60
39. Question
What will be the result of compiling and executing Test class?
package com.skillcertpro.oca;
public class Test {
public static void main(String[] args) {
Double [] arr = new Double[2];
System.out.println(arr[0] + arr[1]);
}
}
Correct
Array elements are initialized to their default values.
arr is referring to an array of Double type, which is reference type and hence both the array elements are initialized to null.
To calculate arr[0] + arr[1], java runtime converts the expression to arr[0].doubleValue() + arr[1].doubleValue().
As arr[0] and arr[1] are null hence calling doubleValue() method throws NullPointerException.
Incorrect
Array elements are initialized to their default values.
arr is referring to an array of Double type, which is reference type and hence both the array elements are initialized to null.
To calculate arr[0] + arr[1], java runtime converts the expression to arr[0].doubleValue() + arr[1].doubleValue().
As arr[0] and arr[1] are null hence calling doubleValue() method throws NullPointerException.
Unattempted
Array elements are initialized to their default values.
arr is referring to an array of Double type, which is reference type and hence both the array elements are initialized to null.
To calculate arr[0] + arr[1], java runtime converts the expression to arr[0].doubleValue() + arr[1].doubleValue().
As arr[0] and arr[1] are null hence calling doubleValue() method throws NullPointerException.
Question 40 of 60
40. Question
What will be the result of compiling and executing Test class?
package com.skillcertpro.oca;
public class Test {
static Boolean[] arr = new Boolean[1];
public static void main(String[] args) {
if(arr[0]) {
System.out.println(true);
} else {
System.out.println(false);
}
}
}
Correct
All the array elements are initialized to their default values. arr is of Boolean type (reference type), so arr[0] is initialized to null.
if expression works with Boolean type variable, so “if(arr[0])” doesn’t cause compilation error but java runtime extracts the boolean value stored in arr[0] and it uses booleanValue() method.
arr[0].booleanValue() means booleanValue() method is invoked on null reference and hence NullPointerException is thrown at runtime.
Incorrect
All the array elements are initialized to their default values. arr is of Boolean type (reference type), so arr[0] is initialized to null.
if expression works with Boolean type variable, so “if(arr[0])” doesn’t cause compilation error but java runtime extracts the boolean value stored in arr[0] and it uses booleanValue() method.
arr[0].booleanValue() means booleanValue() method is invoked on null reference and hence NullPointerException is thrown at runtime.
Unattempted
All the array elements are initialized to their default values. arr is of Boolean type (reference type), so arr[0] is initialized to null.
if expression works with Boolean type variable, so “if(arr[0])” doesn’t cause compilation error but java runtime extracts the boolean value stored in arr[0] and it uses booleanValue() method.
arr[0].booleanValue() means booleanValue() method is invoked on null reference and hence NullPointerException is thrown at runtime.
Question 41 of 60
41. Question
What will be the result of compiling and executing Test class?
package com.skillcertpro.oca;
public class Test {
public static void main(String[] args) {
Boolean b = new Boolean(“tRUe”);
switch(b) {
case true:
System.out.println(“ONE”);
case false:
System.out.println(“TWO”);
default:
System.out.println(“THREE”);
}
}
}
Correct
switch can accept primitive types: byte, short, int, char; wrapper types: Byte, Short, Integer, Character; String and enums.
switch(b) causes compilation failure as b is of Boolean type.
Incorrect
switch can accept primitive types: byte, short, int, char; wrapper types: Byte, Short, Integer, Character; String and enums.
switch(b) causes compilation failure as b is of Boolean type.
Unattempted
switch can accept primitive types: byte, short, int, char; wrapper types: Byte, Short, Integer, Character; String and enums.
switch(b) causes compilation failure as b is of Boolean type.
public class Test {
public static void main(String[] args) {
char c = ‘Z’;
long l = 100_00l;
int i = 9_2;
float f = 2.02f;
double d = 10_0.35d;
l = c + i;
f = c * l * i * f;
f = l + i + c;
i = (int)d;
f = (long)d;
}
}
Does above code compile successfully?
Correct
For readability purpose underscore (_) is used to separate numeric values. This is very useful in representing big numbers such as credit card numbers (1234_7654_9876_0987). long data can be suffixed by l, float by f and double by d. So first 5 variable declaration and assignment statements inside main(String []) method don’t cause any compilation error.
Let’s check rest of the statements:
l = c + i; => Left side variable ‘l’ is of long type and right side expression evaluates to an int value, which can easily be assigned to long type. No compilation error here.
f = c * l * i * f; => Left side variable ‘f’ is of float type and right side expression evaluates to float value, which can easily be assigned to float type. Hence, it compiles successfully.
f = l + i + c; => Left side variable ‘f’ is of float type and right side expression evaluates to long value, which can easily be assigned to float type. Hence, no issues here.
i = (int)d; => double can’t be assigned to int without explicit casting, right side expression `(int)d;` is casting double to int, so no issues.
f = (long)d; => double can’t be assigned to float without explicit casting, right side expression `(long)d;` is casting double to long, which can easily be assigned to float type. It compiles successfully.
Incorrect
For readability purpose underscore (_) is used to separate numeric values. This is very useful in representing big numbers such as credit card numbers (1234_7654_9876_0987). long data can be suffixed by l, float by f and double by d. So first 5 variable declaration and assignment statements inside main(String []) method don’t cause any compilation error.
Let’s check rest of the statements:
l = c + i; => Left side variable ‘l’ is of long type and right side expression evaluates to an int value, which can easily be assigned to long type. No compilation error here.
f = c * l * i * f; => Left side variable ‘f’ is of float type and right side expression evaluates to float value, which can easily be assigned to float type. Hence, it compiles successfully.
f = l + i + c; => Left side variable ‘f’ is of float type and right side expression evaluates to long value, which can easily be assigned to float type. Hence, no issues here.
i = (int)d; => double can’t be assigned to int without explicit casting, right side expression `(int)d;` is casting double to int, so no issues.
f = (long)d; => double can’t be assigned to float without explicit casting, right side expression `(long)d;` is casting double to long, which can easily be assigned to float type. It compiles successfully.
Unattempted
For readability purpose underscore (_) is used to separate numeric values. This is very useful in representing big numbers such as credit card numbers (1234_7654_9876_0987). long data can be suffixed by l, float by f and double by d. So first 5 variable declaration and assignment statements inside main(String []) method don’t cause any compilation error.
Let’s check rest of the statements:
l = c + i; => Left side variable ‘l’ is of long type and right side expression evaluates to an int value, which can easily be assigned to long type. No compilation error here.
f = c * l * i * f; => Left side variable ‘f’ is of float type and right side expression evaluates to float value, which can easily be assigned to float type. Hence, it compiles successfully.
f = l + i + c; => Left side variable ‘f’ is of float type and right side expression evaluates to long value, which can easily be assigned to float type. Hence, no issues here.
i = (int)d; => double can’t be assigned to int without explicit casting, right side expression `(int)d;` is casting double to int, so no issues.
f = (long)d; => double can’t be assigned to float without explicit casting, right side expression `(long)d;` is casting double to long, which can easily be assigned to float type. It compiles successfully.
Question 43 of 60
43. Question
What will be the result of compiling and executing Test class?
package com.skillcertpro.oca;
public class Test {
public static void main(String[] args) {
System.out.println(new Boolean(“ture”));
}
}
Correct
Boolean class code uses equalsIgnoreCase method to validate the passed String, so if passed String is “true” (‘t’, ‘r’, ‘u’ and ‘e’ can be in any case), then boolean value stored in Boolean object is true otherwise false.Â
In this question passed String is “ture” and not “true” and that is why false is printed on to the console.
Incorrect
Boolean class code uses equalsIgnoreCase method to validate the passed String, so if passed String is “true” (‘t’, ‘r’, ‘u’ and ‘e’ can be in any case), then boolean value stored in Boolean object is true otherwise false.Â
In this question passed String is “ture” and not “true” and that is why false is printed on to the console.
Unattempted
Boolean class code uses equalsIgnoreCase method to validate the passed String, so if passed String is “true” (‘t’, ‘r’, ‘u’ and ‘e’ can be in any case), then boolean value stored in Boolean object is true otherwise false.Â
In this question passed String is “ture” and not “true” and that is why false is printed on to the console.
Question 44 of 60
44. Question
What will be the result of compiling and executing Test class?
package com.skillcertpro.oca;
public class Test {
public static void main(String[] args) {
String [] arr = {“abc”, “TrUe”, “false”, null, “FALSE”};
for(String s : arr) {
System.out.print(Boolean.valueOf(s) ? “T” : “F”);
}
}
}
Correct
Boolean.valueOf(String s) returns true if passed String argument is not null and is equal, ignoring case, to the String “true”. In all other cases it returns false.
Boolean.valueOf(“abc”) => false. As “abc”.equalsIgnoreCase(“true”) is false.
Boolean.valueOf(“TrUe”) => true. As “TrUe”.equalsIgnoreCase(“true”) is true.
Boolean.valueOf(“false”) => false. As “false”.equalsIgnoreCase(“true”) is false.
Boolean.valueOf(null) => false. As passed argument is null.
Boolean.valueOf(“FALSE”) => false. As “FALSE”.equalsIgnoreCase(“true”) is false.
Incorrect
Boolean.valueOf(String s) returns true if passed String argument is not null and is equal, ignoring case, to the String “true”. In all other cases it returns false.
Boolean.valueOf(“abc”) => false. As “abc”.equalsIgnoreCase(“true”) is false.
Boolean.valueOf(“TrUe”) => true. As “TrUe”.equalsIgnoreCase(“true”) is true.
Boolean.valueOf(“false”) => false. As “false”.equalsIgnoreCase(“true”) is false.
Boolean.valueOf(null) => false. As passed argument is null.
Boolean.valueOf(“FALSE”) => false. As “FALSE”.equalsIgnoreCase(“true”) is false.
Unattempted
Boolean.valueOf(String s) returns true if passed String argument is not null and is equal, ignoring case, to the String “true”. In all other cases it returns false.
Boolean.valueOf(“abc”) => false. As “abc”.equalsIgnoreCase(“true”) is false.
Boolean.valueOf(“TrUe”) => true. As “TrUe”.equalsIgnoreCase(“true”) is true.
Boolean.valueOf(“false”) => false. As “false”.equalsIgnoreCase(“true”) is false.
Boolean.valueOf(null) => false. As passed argument is null.
Boolean.valueOf(“FALSE”) => false. As “FALSE”.equalsIgnoreCase(“true”) is false.
Question 45 of 60
45. Question
What will be the result of compiling and executing Test class?
package com.skillcertpro.oca;
public class Test {
public static void main(String[] args) {
m(1);
}
There are 3 overloaded methods m. Note all the numeric wrapper classes (Byte, Short, Integer, Long, Float and Double) extend from Number and Number extends from Object.Â
Compiler either does implicit casting or Wrapping but not both. 1 is int literal, Java compiler can’t implicit cast it to double and then box it to Double rather it boxes i to Integer and as Number is the immediate super class of Integer so Number version refers to Integer object.
Number version is printed on to the console.
Incorrect
There are 3 overloaded methods m. Note all the numeric wrapper classes (Byte, Short, Integer, Long, Float and Double) extend from Number and Number extends from Object.Â
Compiler either does implicit casting or Wrapping but not both. 1 is int literal, Java compiler can’t implicit cast it to double and then box it to Double rather it boxes i to Integer and as Number is the immediate super class of Integer so Number version refers to Integer object.
Number version is printed on to the console.
Unattempted
There are 3 overloaded methods m. Note all the numeric wrapper classes (Byte, Short, Integer, Long, Float and Double) extend from Number and Number extends from Object.Â
Compiler either does implicit casting or Wrapping but not both. 1 is int literal, Java compiler can’t implicit cast it to double and then box it to Double rather it boxes i to Integer and as Number is the immediate super class of Integer so Number version refers to Integer object.
Number version is printed on to the console.
Question 46 of 60
46. Question
What will be the result of compiling and executing Test class?
package com.skillcertpro.oca;
public class Test {
public static void main(String[] args) {
extractInt(2.7);
extractInt(2);
}
extractInt method accepts argument of Double type.
extractInt(2.7); => 2.7 is double literal, so Java compiler would box it into Double type. At runtime obj.intValue() would print int portion of the Double data, which is 2.
extractInt(2); => Java compiler either does implicit casting or Wrapping but not both. 2 is int literal, Java compiler can’t implicit cast it to double and then box it to Double. So this statement causes compilation failure.
Incorrect
extractInt method accepts argument of Double type.
extractInt(2.7); => 2.7 is double literal, so Java compiler would box it into Double type. At runtime obj.intValue() would print int portion of the Double data, which is 2.
extractInt(2); => Java compiler either does implicit casting or Wrapping but not both. 2 is int literal, Java compiler can’t implicit cast it to double and then box it to Double. So this statement causes compilation failure.
Unattempted
extractInt method accepts argument of Double type.
extractInt(2.7); => 2.7 is double literal, so Java compiler would box it into Double type. At runtime obj.intValue() would print int portion of the Double data, which is 2.
extractInt(2); => Java compiler either does implicit casting or Wrapping but not both. 2 is int literal, Java compiler can’t implicit cast it to double and then box it to Double. So this statement causes compilation failure.
Question 47 of 60
47. Question
Consider below code of Test.java file:
package com.skillcertprokhattry.oca;
public class Test {
public static void main(String[] args) {
char c1 = ‘a’; //ASCII code of ‘a’ is 97
int i1 = c1; //Line n1
System.out.println(i1); //Line n2
}
}
What is the result of compiling and executing Test class?
Correct
Range of char data type is from 0 to 65535 and hence it can be easily assigned to int type. println() method is overloaded to accept char type and int type both. If char type value is passed, it prints char value and if int type value is passed, it prints int value.
As i1 is of int type, hence corresponding int value, which is 97, is printed on to the console.
Incorrect
Range of char data type is from 0 to 65535 and hence it can be easily assigned to int type. println() method is overloaded to accept char type and int type both. If char type value is passed, it prints char value and if int type value is passed, it prints int value.
As i1 is of int type, hence corresponding int value, which is 97, is printed on to the console.
Unattempted
Range of char data type is from 0 to 65535 and hence it can be easily assigned to int type. println() method is overloaded to accept char type and int type both. If char type value is passed, it prints char value and if int type value is passed, it prints int value.
As i1 is of int type, hence corresponding int value, which is 97, is printed on to the console.
Question 48 of 60
48. Question
Consider below statements:
1. int x = 5____0;
2. int y = ____50;
3. int z = 50____;
4. float f = 123.76_86f;
5. double d = 1_2_3_4;
How many statements are legal?
Correct
For readability purpose underscore (_) is used to separate numeric values. This is very useful in representing big numbers such as credit card numbers (1234_7654_9876_0987). Multiple underscores are also allowed within the digits. Hence, `int x = 5____0;` compiles successfully and variable x stores 50.
`float f = 123.76_86f;` compiles successfully.
1_2_3_4 is int literal 1234 and int can easily be assigned to double, hence `double d = 1_2_3_4;` compiles successfully.
____50 is a valid variable name, and as this variable is not available hence, int y = ____50; causes compilation error.   Â
Underscores must be available within the digits. For the statement int z = 50____; as underscores are used after the digits, hence it causes compilation error.
Incorrect
For readability purpose underscore (_) is used to separate numeric values. This is very useful in representing big numbers such as credit card numbers (1234_7654_9876_0987). Multiple underscores are also allowed within the digits. Hence, `int x = 5____0;` compiles successfully and variable x stores 50.
`float f = 123.76_86f;` compiles successfully.
1_2_3_4 is int literal 1234 and int can easily be assigned to double, hence `double d = 1_2_3_4;` compiles successfully.
____50 is a valid variable name, and as this variable is not available hence, int y = ____50; causes compilation error.   Â
Underscores must be available within the digits. For the statement int z = 50____; as underscores are used after the digits, hence it causes compilation error.
Unattempted
For readability purpose underscore (_) is used to separate numeric values. This is very useful in representing big numbers such as credit card numbers (1234_7654_9876_0987). Multiple underscores are also allowed within the digits. Hence, `int x = 5____0;` compiles successfully and variable x stores 50.
`float f = 123.76_86f;` compiles successfully.
1_2_3_4 is int literal 1234 and int can easily be assigned to double, hence `double d = 1_2_3_4;` compiles successfully.
____50 is a valid variable name, and as this variable is not available hence, int y = ____50; causes compilation error.   Â
Underscores must be available within the digits. For the statement int z = 50____; as underscores are used after the digits, hence it causes compilation error.
Question 49 of 60
49. Question
Given code of Test.java file:
package com.skillcertprokhattry.oca;
public class Test {
  public static void main(String[] args) {
    byte b1 = 10; //Line n1
    int i1 = b1; //Line n2
    byte b2 = i1; //Line n3
    System.out.println(b1 + i1 + b2);
  }
}
What is the result of compiling and executing Test class?
Correct
Let us first check Line n1: byte b1 = 10;
Above statement compiles successfully, even though 10 is an int literal (32 bits) and b1 is of byte primitive type which can store only 8 bits of data.
Here java does some background task, if value of int literal can be easily fit to byte primitive type (-128 to 127), then int literal is implicitly casted to byte type.
So above statement is internally converted to:
byte b1 = (byte)10;
But if you specify any out of range value then it would not be allowed, e.g.
byte b = 128; // It would cause compilation failure as 128 is out of range value for byte type.
There is no issue with Line n2 as byte type (8 bits) can be easily assigned to int type (32 bits).
For line n3, `byte b2 = i1;`, expression on right hand side (i1) is neither a within range literal value nor constant expression, hence it causes compilation failure.
To compile successfully, this expression needs to be explicitly casted, such as: `byte b2 = (byte)i1;`
Incorrect
Let us first check Line n1: byte b1 = 10;
Above statement compiles successfully, even though 10 is an int literal (32 bits) and b1 is of byte primitive type which can store only 8 bits of data.
Here java does some background task, if value of int literal can be easily fit to byte primitive type (-128 to 127), then int literal is implicitly casted to byte type.
So above statement is internally converted to:
byte b1 = (byte)10;
But if you specify any out of range value then it would not be allowed, e.g.
byte b = 128; // It would cause compilation failure as 128 is out of range value for byte type.
There is no issue with Line n2 as byte type (8 bits) can be easily assigned to int type (32 bits).
For line n3, `byte b2 = i1;`, expression on right hand side (i1) is neither a within range literal value nor constant expression, hence it causes compilation failure.
To compile successfully, this expression needs to be explicitly casted, such as: `byte b2 = (byte)i1;`
Unattempted
Let us first check Line n1: byte b1 = 10;
Above statement compiles successfully, even though 10 is an int literal (32 bits) and b1 is of byte primitive type which can store only 8 bits of data.
Here java does some background task, if value of int literal can be easily fit to byte primitive type (-128 to 127), then int literal is implicitly casted to byte type.
So above statement is internally converted to:
byte b1 = (byte)10;
But if you specify any out of range value then it would not be allowed, e.g.
byte b = 128; // It would cause compilation failure as 128 is out of range value for byte type.
There is no issue with Line n2 as byte type (8 bits) can be easily assigned to int type (32 bits).
For line n3, `byte b2 = i1;`, expression on right hand side (i1) is neither a within range literal value nor constant expression, hence it causes compilation failure.
To compile successfully, this expression needs to be explicitly casted, such as: `byte b2 = (byte)i1;`
Question 50 of 60
50. Question
Range of short data type is from -32768 to 32767
Which of the following code segments, written inside main method will compile successfully?
Select ALL that apply.
Correct
Let’s check all the statements one by one:
short s1 = 10;
Above statement compiles successfully, even though 10 is an int literal (32 bits) and s1 is of short primitive type which can store only 16 bits of data.
Here java does some background task, if value of int literal can be easily fit to short primitive type (-32768 to 32767), then int literal is implicitly casted to short type.
So above statement is internally converted to:
short s1 = (short)10;
short s2 = 32768;
It causes compilation failure as 32768 is out of range value.
final int i3 = 10;
short s3 = i3;
Above code compiles successfully. If you are working with final variable and the value is within the range, then final variable is implicitly casted to target type, as in this case i3 is implicitly casted to short.
final int i4 = 40000;
short s4 = i4;
It causes compilation failure as 40000 is out of range value.
final int i5 = 10;
short s5 = i5 + 100;
Above code compiles successfully. If you are working with constant expression and the resultant value of the constant expression is within the range, then resultant value is implicitly casted. In this case, resultant value 110 is implicitly casted.
final int m = 25000;
final int n = 25000;
short s6 = m + n;
m + n is a constant expression but resultant value 50000 is out of range for short type, hence it causes compilation failure.
int i7 = 10;
short s7 = i7;
Compilation error as i7 is non-final variable and hence cannot be implicitly casted to short type.
Incorrect
Let’s check all the statements one by one:
short s1 = 10;
Above statement compiles successfully, even though 10 is an int literal (32 bits) and s1 is of short primitive type which can store only 16 bits of data.
Here java does some background task, if value of int literal can be easily fit to short primitive type (-32768 to 32767), then int literal is implicitly casted to short type.
So above statement is internally converted to:
short s1 = (short)10;
short s2 = 32768;
It causes compilation failure as 32768 is out of range value.
final int i3 = 10;
short s3 = i3;
Above code compiles successfully. If you are working with final variable and the value is within the range, then final variable is implicitly casted to target type, as in this case i3 is implicitly casted to short.
final int i4 = 40000;
short s4 = i4;
It causes compilation failure as 40000 is out of range value.
final int i5 = 10;
short s5 = i5 + 100;
Above code compiles successfully. If you are working with constant expression and the resultant value of the constant expression is within the range, then resultant value is implicitly casted. In this case, resultant value 110 is implicitly casted.
final int m = 25000;
final int n = 25000;
short s6 = m + n;
m + n is a constant expression but resultant value 50000 is out of range for short type, hence it causes compilation failure.
int i7 = 10;
short s7 = i7;
Compilation error as i7 is non-final variable and hence cannot be implicitly casted to short type.
Unattempted
Let’s check all the statements one by one:
short s1 = 10;
Above statement compiles successfully, even though 10 is an int literal (32 bits) and s1 is of short primitive type which can store only 16 bits of data.
Here java does some background task, if value of int literal can be easily fit to short primitive type (-32768 to 32767), then int literal is implicitly casted to short type.
So above statement is internally converted to:
short s1 = (short)10;
short s2 = 32768;
It causes compilation failure as 32768 is out of range value.
final int i3 = 10;
short s3 = i3;
Above code compiles successfully. If you are working with final variable and the value is within the range, then final variable is implicitly casted to target type, as in this case i3 is implicitly casted to short.
final int i4 = 40000;
short s4 = i4;
It causes compilation failure as 40000 is out of range value.
final int i5 = 10;
short s5 = i5 + 100;
Above code compiles successfully. If you are working with constant expression and the resultant value of the constant expression is within the range, then resultant value is implicitly casted. In this case, resultant value 110 is implicitly casted.
final int m = 25000;
final int n = 25000;
short s6 = m + n;
m + n is a constant expression but resultant value 50000 is out of range for short type, hence it causes compilation failure.
int i7 = 10;
short s7 = i7;
Compilation error as i7 is non-final variable and hence cannot be implicitly casted to short type.
Question 51 of 60
51. Question
Consider below code of Test.java file:
package com.skillcertprokhattry.oca;
public class Test {
public static void main(String[] args) {
boolean b1 = 0;
boolean b2 = 1;
System.out.println(b1 + b2);
}
}
What is the result of compiling and executing Test class?
Correct
In Java language, boolean type can store only two values: true and false and these values are not compatible with int type.
Also + operator is not defined for boolean types. Hence, all the 3 statements inside main method causes compilation error.
Incorrect
In Java language, boolean type can store only two values: true and false and these values are not compatible with int type.
Also + operator is not defined for boolean types. Hence, all the 3 statements inside main method causes compilation error.
Unattempted
In Java language, boolean type can store only two values: true and false and these values are not compatible with int type.
Also + operator is not defined for boolean types. Hence, all the 3 statements inside main method causes compilation error.
Question 52 of 60
52. Question
What will be the result of compiling and executing DivModTest class?
package com.skillcertpro.oca;
public class DivModTest {
public static void main(String[] args) {
System.out.println( 23 / 2.0 );
System.out.println( 23 % 2.0 );
}
}
Correct
As floating point numbers are used in the expression, hence result should be in floating point number.
Correct result is:
23 / 2.0 = 11.5Â
23 % 2.0 = 1.0
Incorrect
As floating point numbers are used in the expression, hence result should be in floating point number.
Correct result is:
23 / 2.0 = 11.5Â
23 % 2.0 = 1.0
Unattempted
As floating point numbers are used in the expression, hence result should be in floating point number.
Correct result is:
23 / 2.0 = 11.5Â
23 % 2.0 = 1.0
Question 53 of 60
53. Question
What will be the result of compiling and executing Test class?
package com.skillcertpro.oca;
public class Test {
public static void main(String[] args) {
System.out.println(“Hello” + 1 + 2 + 3 + 4);
}
}
Correct
As expression contains + operator only, which is left to right associative. Let us group the expression.
“Hello” + 1 + 2 + 3 + 4
#VALUE!
#VALUE!
#VALUE!
[Let us solve it now, + operator with String behaves as concatenation operator.]
#VALUE!
#VALUE!
#VALUE!
Hello1234
Incorrect
As expression contains + operator only, which is left to right associative. Let us group the expression.
“Hello” + 1 + 2 + 3 + 4
#VALUE!
#VALUE!
#VALUE!
[Let us solve it now, + operator with String behaves as concatenation operator.]
#VALUE!
#VALUE!
#VALUE!
Hello1234
Unattempted
As expression contains + operator only, which is left to right associative. Let us group the expression.
“Hello” + 1 + 2 + 3 + 4
#VALUE!
#VALUE!
#VALUE!
[Let us solve it now, + operator with String behaves as concatenation operator.]
#VALUE!
#VALUE!
#VALUE!
Hello1234
Question 54 of 60
54. Question
What will be the result of compiling and executing Test class?
package com.skillcertpro.oca;
public class Test {
public static void main(String[] args) {
System.out.println(1 + 2 + 3 + 4 + “Hello”);
}
}
Correct
As expression contains + operator only, which is left to right associative. Let us group the expression.
1 + 2 + 3 + 4 + “Hello”
#VALUE!
#VALUE!
#VALUE!
[Let us solve it now,]
#VALUE!
#VALUE!
#VALUE!
[+ operator with String behaves as concatenation operator.]
= 10Hello
Incorrect
As expression contains + operator only, which is left to right associative. Let us group the expression.
1 + 2 + 3 + 4 + “Hello”
#VALUE!
#VALUE!
#VALUE!
[Let us solve it now,]
#VALUE!
#VALUE!
#VALUE!
[+ operator with String behaves as concatenation operator.]
= 10Hello
Unattempted
As expression contains + operator only, which is left to right associative. Let us group the expression.
1 + 2 + 3 + 4 + “Hello”
#VALUE!
#VALUE!
#VALUE!
[Let us solve it now,]
#VALUE!
#VALUE!
#VALUE!
[+ operator with String behaves as concatenation operator.]
= 10Hello
Question 55 of 60
55. Question
What will be the result of compiling and executing Test class?
package com.skillcertpro.oca;
public class Test {
public static void main(String[] args) {
System.out.println(“Output is: ” + 10 != 5);
}
}
Correct
Binary plus (+) has got higher precedence than != operator. Let us group the expression.Â
“Output is: ” + 10 != 5Â
= (“Output is: ” + 10) != 5Â
[!= is binary operator, so we have to evaluate the left side first. + operator behaves as concatenation operator.]Â
= “Output is: 10” != 5Â
Left side of above expression is String, and right side is int. But String can’t be compared to int hence compilation error.Â
Incorrect
Binary plus (+) has got higher precedence than != operator. Let us group the expression.Â
“Output is: ” + 10 != 5Â
= (“Output is: ” + 10) != 5Â
[!= is binary operator, so we have to evaluate the left side first. + operator behaves as concatenation operator.]Â
= “Output is: 10” != 5Â
Left side of above expression is String, and right side is int. But String can’t be compared to int hence compilation error.Â
Unattempted
Binary plus (+) has got higher precedence than != operator. Let us group the expression.Â
“Output is: ” + 10 != 5Â
= (“Output is: ” + 10) != 5Â
[!= is binary operator, so we have to evaluate the left side first. + operator behaves as concatenation operator.]Â
= “Output is: 10” != 5Â
Left side of above expression is String, and right side is int. But String can’t be compared to int hence compilation error.Â
Question 56 of 60
56. Question
What will be the result of compiling and executing Test class?
package com.skillcertpro.oca;
public class Test {
public static void main(String[] args) {
System.out.println(“Output is: ” + (10 != 5));
}
}
Correct
“Output is: ” + (10 != 5) [Nothing to evaluate at left side, so let’s evaluate the right side of +, 10 != 5 is true.]Â
= “Output is: ” + true [+ operator behaves as concatenation operator]Â
Output is: true
Incorrect
“Output is: ” + (10 != 5) [Nothing to evaluate at left side, so let’s evaluate the right side of +, 10 != 5 is true.]Â
= “Output is: ” + true [+ operator behaves as concatenation operator]Â
Output is: true
Unattempted
“Output is: ” + (10 != 5) [Nothing to evaluate at left side, so let’s evaluate the right side of +, 10 != 5 is true.]Â
= “Output is: ” + true [+ operator behaves as concatenation operator]Â
Output is: true
Question 57 of 60
57. Question
What will be the result of compiling and executing Bonus class?
package com.skillcertpro.oca;
public class Bonus {
public static void main(String[] args) {
int $ = 80000;
String msg = ($ >= 50000) ? “Good bonus” : “Average bonus”;
System.out.println(msg);
}
}
Correct
$ is valid identifier. $ = 80000Â
This is an example of ternary operator. First operand ($ >= 50000) is a boolean expression which is true, as 80000 >= 50000 is true.Â
msg will refer to “Good bonus”.
Incorrect
$ is valid identifier. $ = 80000Â
This is an example of ternary operator. First operand ($ >= 50000) is a boolean expression which is true, as 80000 >= 50000 is true.Â
msg will refer to “Good bonus”.
Unattempted
$ is valid identifier. $ = 80000Â
This is an example of ternary operator. First operand ($ >= 50000) is a boolean expression which is true, as 80000 >= 50000 is true.Â
msg will refer to “Good bonus”.
Question 58 of 60
58. Question
What will be the result of compiling and executing Test class?
package com.skillcertpro.oca;
public class Test {
public static void main(String[] args) {
int a = 20;
int var = –a * a++ + a– – –a;
System.out.println(“a = ” + a);
System.out.println(“var = ” + var);
}
}
Correct
int var = –a * a++ + a– – –a;
int var = –a * (a++) + (a–) – –a;
int var = (–a) * (a++) + (a–) – (–a);
int var = ((–a) * (a++)) + (a–) – (–a);
int var = (((–a) * (a++)) + (a–)) – (–a);
int var = ((19 * (a++)) + (a–)) – (–a); //a = 19
int var = ((19 * 19) + (a–)) – (–a); //a = 20
int var = (361 + 20) – (–a); //a = 19
int var = 381 – (–a); //a = 19
int var = 381 – 18; //a = 18
int var = 363 // a = 18
So,
a = 18
var = 363
Incorrect
int var = –a * a++ + a– – –a;
int var = –a * (a++) + (a–) – –a;
int var = (–a) * (a++) + (a–) – (–a);
int var = ((–a) * (a++)) + (a–) – (–a);
int var = (((–a) * (a++)) + (a–)) – (–a);
int var = ((19 * (a++)) + (a–)) – (–a); //a = 19
int var = ((19 * 19) + (a–)) – (–a); //a = 20
int var = (361 + 20) – (–a); //a = 19
int var = 381 – (–a); //a = 19
int var = 381 – 18; //a = 18
int var = 363 // a = 18
So,
a = 18
var = 363
Unattempted
int var = –a * a++ + a– – –a;
int var = –a * (a++) + (a–) – –a;
int var = (–a) * (a++) + (a–) – (–a);
int var = ((–a) * (a++)) + (a–) – (–a);
int var = (((–a) * (a++)) + (a–)) – (–a);
int var = ((19 * (a++)) + (a–)) – (–a); //a = 19
int var = ((19 * 19) + (a–)) – (–a); //a = 20
int var = (361 + 20) – (–a); //a = 19
int var = 381 – (–a); //a = 19
int var = 381 – 18; //a = 18
int var = 363 // a = 18
So,
a = 18
var = 363
Question 59 of 60
59. Question
What will be the result of compiling and executing Test class?
package com.skillcertpro.oca;
public class Test {
public static void main(String[] args) {
int a = 7;
boolean res = a++ == 7 && ++a == 9 || a++ == 9;
System.out.println(“a = ” + a);
System.out.println(“res = ” + res);
}
}
What will be the result of compiling and executing the Test class?
package com.skillcertpro.oca;
public class Test {
public static void main(String[] args) {
int grade = 75;
if(grade > 60)
System.out.println(“Congratulations”);
System.out.println(“You passed”);
else
System.out.println(“You failed”);
}
}
Correct
As there is no brackets after if, hence only one statement is part of if block and other is outside.
Above code can be written as below:
if(grade > 60) {
System.out.println(“Congratulations”);
}
System.out.println(“You passed”);
else
System.out.println(“You failed”);
There should not be anything between if-else block but in this case, System.out.println(“You passed”); is between if-else and thus Compilation error.
As there is no brackets after if, hence only one statement is part of if block and other is outside.
Above code can be written as below:
if(grade > 60) {
System.out.println(“Congratulations”);
}
System.out.println(“You passed”);
else
System.out.println(“You failed”);
There should not be anything between if-else block but in this case, System.out.println(“You passed”); is between if-else and thus Compilation error.
Incorrect
As there is no brackets after if, hence only one statement is part of if block and other is outside.
Above code can be written as below:
if(grade > 60) {
System.out.println(“Congratulations”);
}
System.out.println(“You passed”);
else
System.out.println(“You failed”);
There should not be anything between if-else block but in this case, System.out.println(“You passed”); is between if-else and thus Compilation error.
As there is no brackets after if, hence only one statement is part of if block and other is outside.
Above code can be written as below:
if(grade > 60) {
System.out.println(“Congratulations”);
}
System.out.println(“You passed”);
else
System.out.println(“You failed”);
There should not be anything between if-else block but in this case, System.out.println(“You passed”); is between if-else and thus Compilation error.
Unattempted
As there is no brackets after if, hence only one statement is part of if block and other is outside.
Above code can be written as below:
if(grade > 60) {
System.out.println(“Congratulations”);
}
System.out.println(“You passed”);
else
System.out.println(“You failed”);
There should not be anything between if-else block but in this case, System.out.println(“You passed”); is between if-else and thus Compilation error.
As there is no brackets after if, hence only one statement is part of if block and other is outside.
Above code can be written as below:
if(grade > 60) {
System.out.println(“Congratulations”);
}
System.out.println(“You passed”);
else
System.out.println(“You failed”);
There should not be anything between if-else block but in this case, System.out.println(“You passed”); is between if-else and thus Compilation error.
X
Use Page numbers below to navigate to other practice tests