• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer
  • Core Java
  • Design Patterns
  • JSP
  • Servlets
  • Building Tools
  • jQuery
  • Spring
  • Hibernate
  • Mongo DB
  • More
    • HTML
    • SCJP
    • AJAX
    • UML
    • Struts
    • J2EE
    • Testing
    • Angular JS

J2EE Reference

  • Home
  • About Us
    • Java Learning Centers
  • Contact Us

JAVA QUIZ

January 28, 2013 By j2eereference Leave a Comment

JAVA QUIZ

Start
Congratulations - you have completed JAVA QUIZ. You scored %%SCORE%% out of %%TOTAL%%. Your performance has been rated as %%RATING%%
Your answers are highlighted below.
Question 1
1. public class Foo {

2. public static void main(String[] args) {

3. try {

4. return;

5. } finally {

6. System.out.println( "Finally" );

7. }

8. }

9. }
What is the result?
A
Finally
B
Compilation Fails
C
The code runs with no output.
D
An exception is thrown at runtime.
Question 2
ClassOne.java:

1. package com.abe.pkg1;

2. public class ClassOne {

3. private char var = 'a';

4. char getVar() { return var; }

5. }

ClassTest.java:

1. package com.abe.pkg2;

2. import com.abc.pkg1.ClassOne;

3. public class ClassTest extends ClassOne {

4. public static void main(String[] args) {

5. char a = new ClassOne().getVar();

6. char b = new ClassTest().getVar();

7. }

8. }

 
What is the result?
A
Compilation fails.
B
Compilation succeeds and no exceptions are thrown.
C
An exception is thrown at line 5 in ClassTest.java.
D
An exception is thrown at line 6 in ClassTest.java.
Question 3
1. public class Alpha1 {

2. public static void main( String[] args ) {

3. boolean flag; int i=0;

5. do {

6. flag = false;

7. System.out.println( i++ );

8. flag = i < 10;

9. continue;

10. } while ( (flag)? true:false );

11. }

12. }
What is the result?
A
000000000
B
0123456789
C
Compilation fails.
D
The code runs with no output.
E
The code enters an infinite loop.
Question 4
1. package foo;

2.

3. import java.util.Vector;

4.

5. protected class MyVector Vector {

6. init i = 1;

7. public MyVector() {

8. i = 2;

9. }

10. }

11.

12. public class MyNewVector extends MyVector {

13. public MyNewVector() {

14. i = 4;

15. }

16. public static void main(String args[]) {

17. MyVector v = new MyNewVector();

18. }

19. }
What is the result?
A
Compilation succeeds.
B
Compilation fails because of an error at line 5.
C
Compilation fails because of an error at line 6.
D
Compilation fails because of an error at line 14.
E
Compilation fails because of an error at line 17.
Question 5
What is the output for the below code ? 

1.         public class A {

2.         int add(int i, int j){

3.                     return i+j;

4.                     }

5.         }

6.         public class B extends A{

7.         public static void main(String argv[]){

8.         short s = 9;

9.         System.out.println(add(s,6));

10.        }

11.        }
Suggest an answer:
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
Compile fail due to error on line no 15
Question 6
What is the output for the below code ?

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();

}

}
A
0 false 0
B
0 true 0
C
0 0 0
D
Compile error – static variable must be initialized before use.
Question 6 Explanation: 
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 7
What is the output for the below code ?

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._$);

}

}
A
7 9 0
B
7 0 0
C
Compile error – $7 is not valid identifier.
D
Compile error – do is not valid identifier.
Question 7 Explanation: 
$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 8
What is the output for the below code ?

 

package com;

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”);

}

}

 

package exam;

import com.Animal;

public class Test {

public static void main(String[] args){

Animal a = new Cat();

a.printName();

}

}
A
Animal
B
Cat
C
Animal Cat
D
Compile Error
Question 8 Explanation: 
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 9
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);

}

}
A
Value-B 11
B
Value-B 10
C
Value-A 11
D
Value-A 10
Question 9 Explanation: 
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 10
What is the output for the below code ?

 

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();

}
A
A block static
B
static block A
C
static A
D
A
Question 11
What is the output for the below code ?

1. public class Test {

2. public static void main(String[] args){

3. int i = 010;

4. int j = 07;

5. System.out.println(i);

6. System.out.println(j);

7. }

8. }

A
8 7
B
10 7
C
Compilation fails with an error at line 3
D
Compilation fails with an error at line 5
Question 11 Explanation: 
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 12
What is the output for the below code ?

1. public class Test {

2. public static void main(String[] args){

3. byte b = 6;

4. b+=8;

5. System.out.println(b);

6. b = b+7;

7. System.out.println(b);

8. }

9. }
A
14 21
B
14 13
C
Compilation fails with an error at line 6
D
Compilation fails with an error at line 4
Question 13
What is the output for the below code ?

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”;

}
}
A
abc
B
xyz
C
Compilation fails
D
Compilation clean but no output
Question 14
What is the output for the below code ?

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);

}

}
A
long
B
int
C
Compilation fails.
D
Compilation clean but throws RuntimeException
Question 14 Explanation: 
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 15
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.

cd javaproject

Which of the below command puts Test.class file inside classes folder ?
A
javac -d classes src/Test.java
B
javac Test.java
C
javac src/Test.java
D
javac classes src/Test.java
Question 15 Explanation: 
The -d option lets you tell the compiler in which directory to put the .class file it generates (d for destination)
Question 16
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”);

}

}

Test1.java source code is :

public class Test1{

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 ?
A
Hello Test
B
Hello Test Hello Test1
C
Hello Test1
D
Run fails – class not found
Question 17
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 follwing command set classpath to compile clean?

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
Question 17 Explanation: 
javac -classpath exam/app.jar Test.java is the correct command to set exam/app.jar in classpath
Question 18
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);

}

}
A
Overridden: hello (String, Integer[])
B
hello (String, Integer[])
C
Compilation fails
D
None of the above
Question 18 Explanation: 
Unhandled exception type Exception.
Question 19
What happens when the following code is compiled and run.

Select the one correct answer.

for(int i = 2; i < 4; i++)

for(int j = 2; j < 4; j++)

if(i < j)

assert i!=j : i; 
A
The class compiles and runs, but does not print anything.
B
The number 2 gets printed with AssertionError
C
Compile error
D
The number 3 gets printed with AssertionError
Question 19 Explanation: 
When if condition returns true, the assert statement also returns true. Hence AssertionError does not get generated.
Question 20
try{

File f = new File(“a.txt”);

}catch(Exception e){

}catch(IOException io){

}

Is this code create new file name a.txt ?
A
True
B
False
C
Compilation error
D
None
Question 20 Explanation: 
IOException is unreachable to compiler because all exception is going to catch by Exception block.
Once you are finished, click the button below. Any items you have not completed will be marked incorrect. Get Results
There are 20 questions to complete.
←
List
→
Return
Shaded items are complete.
12345
678910
1112131415
1617181920
End
Return
You have completed
questions
question
Your score is
Correct
Wrong
Partial-Credit
You have not finished your quiz. If you leave this page, your progress will be lost.
Correct Answer
You Selected
Not Attempted
Final Score on Quiz
Attempted Questions Correct
Attempted Questions Wrong
Questions Not Attempted
Total Questions on Quiz
Question Details
Results
Date
Score
Hint
Time allowed
minutes
seconds
Time used
Answer Choice(s) Selected
Question Text
All done
Need more practice!
Keep trying!
Not bad!
Good work!
Perfect!

Related Posts

  • Text Blocks – Feature added in Java17
  • Record – The new feature added in java 17
  • What is Sealed Class
  • Features added in Java 17
  • Java Buzzwords
  • Anonymous Inner Class in Java
  • Network Programming – java.net Package
  • Java Regular Expressions
  • Method Local Inner Class in Java
  • URL Processing in Java

Filed Under: Core Java

Reader Interactions

Leave a Reply Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

FOLLOW US ONLINE

  • View J2eereference-166104970118637’s profile on Facebook
  • View j2eereference’s profile on Twitter
  • View j2eereference’s profile on LinkedIn

Subscribe by email

Recent posts

  • What is parallel Stream
  • reduce method of the Stream class
  • Difference between the findFirst() and findAny() method
  • intern() method of String class
  • SOLID – Five principles of object-oriented software design
  • Java Coding Best Practices
  • How to use lambda expression effectively
  • Enhanced pseudo-Random Number Generators in java17
  • How to use Foreign-Memory Access API
  • Pattern Matching for instanceof
  • Text Blocks – Feature added in Java17
  • Record – The new feature added in java 17
  • What is Sealed Class
  • Features added in Java 17
  • Java Buzzwords

Footer

Core Java
Design Patterns
JSP
Servlets
HTML
Building Tools
AJAX
SCJP
jQuery
Testing
Spring
UML
Struts
Java Centers
Java Training
Home
About Us
Contact Us
Copyright © j2eereference.com. All right reserved.