OCJP Preparation
What is the output for the below code ?
Question 1.
public class A {
int add(int i, int j) {
return i + j;
}
}
public class B extends A {
public static void main(String argv[]) {
short s = 9;
System.out.println(add(s, 6));
}
}
int add(int i, int j) {
return i + j;
}
}
public class B extends A {
public static void main(String argv[]) {
short s = 9;
System.out.println(add(s, 6));
}
}
Options are:
A. Compile fail due to error on line no 2
B. Compile fail due to error on line no 9
C. Compile fail due to error on line no 8
D. 15
Answer :
B is the correct answer.
Cannot make a static reference to the non-static method add(int, int) from the type A. The short s is autoboxed correctly, but the add() method cannot be invoked from a static method because add() method is not static.
Question 2.
public class A {
int k;
boolean istrue;
static int p;
public void printValue() {
System.out.print(k);
System.out.print(istrue);
System.out.print(p);
}
}
public class Test {
public static void main(String argv[]) {
A a = new A();
a.printValue();
}
}
int k;
boolean istrue;
static int p;
public void printValue() {
System.out.print(k);
System.out.print(istrue);
System.out.print(p);
}
}
public class Test {
public static void main(String argv[]) {
A a = new A();
a.printValue();
}
}
Options are:
A.0 false 0
B.0 true 0
C.0 0 0
D.Compile error – static variable must be initialized before use.
Answer :
A is the correct answer.
Global and static variable need not be initialized before use. Default value of global and static int variable is zero. Default value of boolean variable is false. Remember local variable must be initialized before use.
Question 3.
public class Test {
int _$;
int $7;
int do;
public static void main(String argv[]) {
Test test = new Test();
test.$7 = 7;
test.do = 9;
System.out.println(test.$7);
System.out.println(test.do);
System.out.println(test._$);
}
}
int _$;
int $7;
int do;
public static void main(String argv[]) {
Test test = new Test();
test.$7 = 7;
test.do = 9;
System.out.println(test.$7);
System.out.println(test.do);
System.out.println(test._$);
}
}
Options are:
A.7 9 0
B.7 0 0
C.Compile error – $7 is not valid identifier.
D.Compile error – do is not valid identifier.
Answer:
D is the correct answer. $7 is valid identifier. Identifiers must start with a letter, a currency character ($), or underscore ( _ ). Identifiers cannot start with a number. You can’t use a Java keyword as an identifier. do is a Java keyword.
Question 4.
package com;
class Animal {
public void printName() {
System.out.println("Animal");
}
}
class Animal {
public void printName() {
System.out.println("Animal");
}
}
package exam;
import com.Animal;
public class Cat extends Animal {
public void printName() {
System.out.println("Cat");
}
}
import com.Animal;
public class Cat extends Animal {
public void printName() {
System.out.println("Cat");
}
}
package exam;
import com.Animal;
public class Test {
public static void main(String[] args) {
Animal a = new Cat();
a.printName();
}
}
import com.Animal;
public class Test {
public static void main(String[] args) {
Animal a = new Cat();
a.printName();
}
}
Options are:
A.Animal
B.Cat
C.Animal Cat
D.Compile Error
Answer :
D is the correct answer.
Cat class won’t compile because its superclass, Animal, has default access and is in a different package. Only public superclass can be accessible for different package.
Question 5.
public class A {
int i = 10;
public void printValue() {
System.out.println("Value-A");
}
}
public class B extends A {
int i = 12;
public void printValue() {
System.out.print("Value-B");
}
}
public class Test {
public static void main(String argv[]) {
A a = new B();
a.printValue();
System.out.println(a.i);
}
}
int i = 10;
public void printValue() {
System.out.println("Value-A");
}
}
public class B extends A {
int i = 12;
public void printValue() {
System.out.print("Value-B");
}
}
public class Test {
public static void main(String argv[]) {
A a = new B();
a.printValue();
System.out.println(a.i);
}
}
Options are:
A.Value-B 11
B.Value-B 10
C.Value-A 10
D.Value-A 11
Answer:
B is the correct answer.
If you create object of subclass with reference of super class like ( A a = new B();) then subclass method and super class variable will be executed.
Question 6.
public enum Test {
BREAKFAST(7, 30), LUNCH(12, 15), DINNER(19, 45);
private int hh;
private int mm;
Test(int hh, int mm) {
assert (hh >= 0 && hh <= 23) : "Illegal hour.";
assert (mm >= 0 && mm <= 59) : "Illegal mins.";
this.hh = hh;
this.mm = mm;
}
public int getHour() {
return hh;
}
public int getMins() {
return mm;
}
public static void main(String args[]){
Test t = new BREAKFAST;
System.out.println(t.getHour() +":"+t.getMins());
}
}
BREAKFAST(7, 30), LUNCH(12, 15), DINNER(19, 45);
private int hh;
private int mm;
Test(int hh, int mm) {
assert (hh >= 0 && hh <= 23) : "Illegal hour.";
assert (mm >= 0 && mm <= 59) : "Illegal mins.";
this.hh = hh;
this.mm = mm;
}
public int getHour() {
return hh;
}
public int getMins() {
return mm;
}
public static void main(String args[]){
Test t = new BREAKFAST;
System.out.println(t.getHour() +":"+t.getMins());
}
}
Options are:
A.7:30
B.Compile Error – an enum cannot be instantiated using the new operator.
C.12:30
D.19:45
Answer:
B is the correct answer.
As an enum cannot be instantiated using the new operator, the constructors cannot be called explicitly. You have to do like Test t = BREAKFAST;
Question 7.
public class A {
static {
System.out.println("static:");
}
{
System.out.println("block");
}
public A() {
System.out.println("A");
}
public static void main(String[] args) {
A a = new A();
}
}
static {
System.out.println("static:");
}
{
System.out.println("block");
}
public A() {
System.out.println("A");
}
public static void main(String[] args) {
A a = new A();
}
}
Options are:
A.A block static
B.static block A
C.static A
D.A
Answer:
B is the correct answer.
First execute static block, then statement block then constructor.
Question 8.
public class Test {
public static void main(String[] args) {
int i = 010;
int j = 07;
System.out.println(i);
System.out.println(j);
}
}
public static void main(String[] args) {
int i = 010;
int j = 07;
System.out.println(i);
System.out.println(j);
}
}
Options are:
A.8 7
B.10 7
C.Compilation fails with an error at line 3
D.Compilation fails with an error at line 5
Answer:
A is the correct answer.
By placing a zero in front of the number is an integer in octal form. 010 is in octal form .so its value is 8.
Question 9.
public class Test {
public static void main(String[] args) {
byte b = 6;
b += 8;
System.out.println(b);
b = b + 7;
System.out.println(b);
}
}
public static void main(String[] args) {
byte b = 6;
b += 8;
System.out.println(b);
b = b + 7;
System.out.println(b);
}
}
Options are:
A.14 21
B.14 13
C.Compilation fails with an error at line 6
D.Compilation fails with an error at line 4
Answer:
C is the correct answer.
int or smaller expressions always resulting in an int. So compiler complain about Type
mismatch: cannot convert from int to byte for b = b+7; But b += 7; // No problem
because +=, -=, *=, and /= will all put in an implicit cast. b += 7 is same as b = (byte)b+7
so compiler not complain.
Question 10.
public class Test {
public static void main(String[] args) {
String value = "abc";
changeValue(value);
System.out.println(value);
}
public static void changeValue(String a) {
a = "xyz";
}
}
public static void main(String[] args) {
String value = "abc";
changeValue(value);
System.out.println(value);
}
public static void changeValue(String a) {
a = "xyz";
}
}
Options are:
A.abc
B.xyz
C.Compilation fails
D.Compilation clean but no output
Answer:
A is the correct answer.
Java pass reference as value. passing the object reference, and not the actual object itself.
Simply reassigning to the parameter used to pass the value into the method will do
nothing, because the parameter is essentially a local variable.
Question 11.
public class Test {
public static void printValue(int i, int j, int k) {
System.out.println("int");
}
public static void printValue(byte... b) {
System.out.println("long");
}
public static void main(String... args) {
byte b = 9;
printValue(b, b, b);
}
}
public static void printValue(int i, int j, int k) {
System.out.println("int");
}
public static void printValue(byte... b) {
System.out.println("long");
}
public static void main(String... args) {
byte b = 9;
printValue(b, b, b);
}
}
Options are:
A.long
B.int
C.Compilation fails
D.Compilation clean but throws RuntimeException
Answer:
B is the correct answer.
Primitive widening uses the smallest method argument possible. (For Example if you pass short value to a method but method with short argument is not available then compiler choose method with int argument). But in this case compiler will prefer the older style before it chooses the newer style, to keep existing code more robust. var-args method is looser than widen.
Question 12.
You have a java file name Test.java inside src folder of javaproject directory. You have also classes
folder inside javaproject directory. you have issued below command from command prompt.
folder inside javaproject directory. you have issued below command from command prompt.
cd javaproject
Which of the below command puts Test.class file inside classes folder ?
Options are:
A.javac -d classes src/Test.java
B.javac Test.java
C.javac src/Test.java
D.javac classes src/Test.java
Answer:
A is the correct answer.
The -d option lets you tell the compiler in which directory to put the .class file it
generates (d for destination)
Question 13.
You have two class files name Test.class and Test1.class inside javaproject directory.
Test.java source code is :
public class Test {
public static void main(String[] args) {
System.out.println("Hello Test");
}
}
public static void main(String[] args) {
System.out.println("Hello Test");
}
}
Test1.java source code is :
public static void main(String[] args) {
System.out.println("Hello Test1");
}
}
you have issued below commands from command prompt.
cd javaproject
java Test Test1
What is the output ?
Options are:
A.Hello Test
B.Hello Test Hello Test1
C.Hello Test1
D.Run fails – class not found
Answer:
A is the correct answer.
You must specify exactly one class file to execute. If more than one then first one will be executed.
Question 14.
You have a java file name Test.java .
Test.java needs access to a class contained in app.jar in “exam”
directory.
Which of the following command set classpath to compile clean?
Options are:
A.javac -classpath exam/app.jar Test.java
B.javac -classpath app.jar Test.java
C.javac -classpath exam Test.java
D.None of the above
Answer:
A is the correct answer.
javac -classpath exam/app.jar Test.java is the correct command to set exam/app.jar in classpath.
Question 15.
What will be the result of compiling the following code:
public class SuperClass {
public int doIt(String str, Integer... data) throws Exception {
String signature = "(String, Integer[])";
System.out.println(str + " " + signature);
return 1;
}
}
public class SubClass extends SuperClass {
public int doIt(String str, Integer... data) {
String signature = "(String, Integer[])";
System.out.println("Overridden: " + str + " " + signature);
return 0;
}
public static void main(String... args) {
SuperClass sb = new SubClass();
sb.doIt("hello", 3);
}
}
public int doIt(String str, Integer... data) throws Exception {
String signature = "(String, Integer[])";
System.out.println(str + " " + signature);
return 1;
}
}
public class SubClass extends SuperClass {
public int doIt(String str, Integer... data) {
String signature = "(String, Integer[])";
System.out.println("Overridden: " + str + " " + signature);
return 0;
}
public static void main(String... args) {
SuperClass sb = new SubClass();
sb.doIt("hello", 3);
}
}
Options are:
A.Overridden: hello (String, Integer[])
B.hello (String, Integer[])
C.Complilation fails
D.None of the above
Answer:
C is the correct answer.
Unhandled exception type Exception.
Question 16:
What will be the result of following code:
public MyException1() {
}
}
class MyException2 extends Exception {
public MyException2() {
}
}
class ExceptionTest {
public static void main(String[] args) throws Exception {
try {
m2();
} finally {
m3();
} catch (MyException2 e) {
}
}
public static void m2() throws MyException1 {
throw new MyException1();
}
public static void m3() throws MyException2 {
throw new MyException2();
}
}
Options:
A. It will compile but will throw MyException1 when run.
B. It will compile but will throw MyException2 when run.
C. It will compile and run without throwing any exceptions.
D. It will not compile.
Answer: D
Explaination : catch block will come after try block
Your good knowledge and kindness in playing with all the pieces were very useful. I don’t know what I would have done if I had not encountered such a step like this.
ReplyDeletehttps://bit.ly/2JbBPth
Really you have done great job,There are may person searching about that now they will find enough resources by your post
ReplyDeleteData Science Training in Chennai
Data science training in bangalore
Data science online training
Data science training in pune
Data science training in kalyan nagar
Data science training in Bangalore
Data science training in tambaram
A very nice guide. I will definitely follow these tips. Thank you for sharing such detailed article. I am learning a lot from you.
ReplyDeleteDevops training in velachry
Devops training in OMR
Deops training in annanagar
Devops training in chennai
Devops training in marathahalli
Devops training in rajajinagar
Devops training in BTM Layout
Great post! I am actually getting ready to across this information, It’s very helpful for this blog.Also great with all of the valuable information you have Keep up the good work you are doing well.
ReplyDeleteDevops Training in Chennai
Devops Training in Bangalore
Devops Training in pune
Devops training in tambaram
Devops training in velachery
This is a good post. This post give truly quality information. I’m definitely going to look into it. Really very useful tips are provided here. thank you so much. Keep up the good works.
ReplyDeletejava training in chennai | java training in bangalore
java training in tambaram | java training in velachery
java training in omr
This comment has been removed by the author.
ReplyDeleteAfter seeing your article I want to say that the presentation is very good and also a well-written article with some very good information which is very useful for the readers....thanks for sharing it and do share more posts like this.
ReplyDeletepython training institute in chennai
python training in velachery
python training institute in chennai
ReplyDeleteThanks for your informative article, Your post helped me to understand the future and career prospects & Keep on updating your blog with such awesome article.
angularjs Training in bangalore
angularjs Training in electronic-city
angularjs Training in online
angularjs Training in marathahalli
Were a gaggle of volunteers as well as starting off a brand new gumption within a community. Your blog furnished us precious details to be effective on. You've got completed any amazing work!
ReplyDeleteData Science course in kalyan nagar | Data Science course in OMR
Data Science course in chennai | Data science course in velachery
Data science online course | Data science course in jaya nagar
Greetings. I know this is somewhat off-topic, but I was wondering if you knew where I could get a captcha plugin for my comment form? I’m using the same blog platform like yours, and I’m having difficulty finding one? Thanks a lot.
ReplyDeleteAmazon Web Services Training in Tambaram, Chennai|Best AWS Training in Tambaram, Chennai
Amazon Online Training
AWS Training in JayaNagar | Amazon Web Services Training in jayaNagar
AWS Training in Rajaji Nagar | Amazon Web Services Training in Rajaji Nagar
Amazon Web Services Training in Pune | Best AWS Training in Pune
AWS Online Training | Online AWS Certification Course - Gangboard
I would assume that we use more than the eyes to gauge a person's feelings. Mouth. Body language. Even voice. You could at least have given us a face in this test.
ReplyDeleteangularjs-Training in pune
angularjs-Training in chennai
angularjs Training in chennai
angularjs-Training in tambaram
angularjs-Training in sholinganallur
Great Article… I love to read your articles because your writing style is too good, its is very very helpful for all of us and I never get bored while reading your article because, they are becomes a more and more interesting from the starting lines until the end.
ReplyDeleteselenium training in electronic city | selenium training in electronic city | Selenium Training in Chennai | Selenium online Training | Selenium Training in Pune | Selenium Training in Bangalore
Your new valuable key points imply much a person like me and extremely more to my office workers. With thanks; from every one of us.
ReplyDeletenebosh course in chennai
I’m planning to start my blog soon, but I’m a little lost on everything. Would you suggest starting with a free platform like Word Press or go for a paid option?
ReplyDeleteiosh safety course in chennai
Thanks for sharing such a great blog Keep posting..
ReplyDeleteCCNA Training in Delhi
CCNA institute in Delhi
This comment has been removed by the author.
ReplyDeleteGood post!Thank you so much for sharing this pretty post,it was so good to read and useful to improve my knowledge as updated one,keep blogging.
ReplyDeleteCCNA Training in Electronic City
Ijust got to this amazing site not long ago. I was actually captured with the piece of resources you have got here. Big thumbs up for making such wonderful blog page!I recently came across your article and have been reading along. I want to express my admiration of your writing skill and ability to make readers read from the beginning to the end. I would like to read newer posts and to share my thoughts with you.
ReplyDeleteData Science Training In Chennai
Data Science Online Training In Chennai
Data Science Training In Bangalore
Data Science Training In Hyderabad
Data Science Training In Coimbatore
Data Science Training
Data Science Online Training
"Hi, I have just started to Learn Java Certification Online. and this blog is really informative for me. Thank you for this blog!"
ReplyDeleteGreat Post.Thanks for sharing.
ReplyDeleteJava Classes in Nagpur