Welcome Guest | Sign in | Register

Home > Java Programming > Variables and Loops > Questions and Answers

01. 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.}
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 and Explanation

Answer: Compile fail due to error on line no 9

Explanation:
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.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
02. 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. 000 D. Compile error - static variable must be initialized before use.

Answer and Explanation

Answer: 0 false 0

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.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
03. 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.

Answer and Explanation

Answer: Compile error - do is not valid identifier.

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.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
04. 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

Answer and Explanation

Answer: static block A

Explanation:
First execute static block, then statement block then constructor.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
05. 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

Answer and Explanation

Answer: 8 7

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.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
06. 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

Answer and Explanation

Answer: Compilation fails with an error at line 6

Explanation:
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.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
07. 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

Answer and Explanation

Answer: javac -d classes src/Test.java

Explanation:
The -d option lets you tell the compiler in which directory to put the .class file it
generates (d for destination)

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
08. 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

Answer and Explanation

Answer: Hello Test

Explanation:
You must specify exactly one class file to execute. If more than one then first one will be executed.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
09. 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

Answer and Explanation

Answer: javac -classpath exam/app.jar Test.java

Explanation:
javac -classpath exam/app.jar Test.java is the correct command to set exam/app.jar in classpath.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
10. 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

Answer and Explanation

Answer: The class compiles and runs, but does not print anything

Explanation:
When if condition returns true, the assert statement also returns true. Hence
AssertionError does not get generated. .

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum



Partner Sites
LucentBlackBoard.com                  SoftLucent.com                  LucentJobs.com
All rights reserved 2012-2015 SoftLucent.