Java 7 Mock Exam Questions
-------------------------------------------------------------------------------------------
Que 1:
What is the result of compiling and running the following code?
public class Tester {
public static void main(String[] args) {
String str = "java";
StringBuffer sb = new StringBuffer("javachamp");
sb.insert(9, ".com");
str.concat("champ");
if (sb.length() < 6 || str.equals("javachamp")) {
System.out.print(sb);
}
sb.delete(2, 7);
System.out.print(sb);
}
Please choose only one answer:
A. Javachamp.comjamp.comB.jamp
C. jamp.com
D.javachampjamp
E.jap
------------------------------------------------------------------------
Que 2:
What will be the Output?------------------------------------------------------------------------
Que 2:
public class InnerClassThis {
int x=10;
public void foo() {
System.out.println("Outer class");
}
public class ReallyInner {
public void foo() {
System.out.println("Inner class");
}
public void test() {
this.foo();
InnerClassThis.this.foo();
}
}
public static void main(String... args) {
InnerClassThis.ReallyInner i = new InnerClassThis().new ReallyInner();
i.test();
}
}
Please choose only one answer:
A. The code does not compile.
B. Inner class
C. Outer class
E.The code compiles but throws runtime exception
F.Outer class
--------------------------------------------------------------------
QUE.3
Which is true? (Choose all that apply.)
A. "X extends Y" is correct if and only if X is a class and Y is an interface
B. "X extends Y" is correct if and only if X is an interface and Y is a class
C. "X extends Y" is correct if X and Y are either both classes or both interfaces
D. "X extends Y" is correct for all combinations of X and Y being classes and/or interfaces
C is correct.
------------------------------------------------------------------------
------------------------------------------------------------------------
QUE.4
Which method names follow the JavaBeans standard? (Choose all that apply.)
A. addSize
B. getCust
C. deleteRep
D. isColorado
E. putDimensions
Ans
B and D use the valid prefixes 'get' and 'is'.
------------------------------------------------------------------------
QUE.5
Given:
1. class Voop {
2. public static void main(String[] args) {
3. doStuff(1);
4. DOsTUFF(1,2);
5. }
6. // INSERT CODE HERE
7.}
Which inserted independently at line 6 will compile(Choose all that apply.)
A. static void doStuff(int... doArgs) { }
B. static void doStuff(int[] doArgs) { }
C. static void doStuff(int doArgs...) { }
D. static void doStuff(int... doArgs, int y) { }
E. static void doStuff(int x, int... doArgs) { }
Ans.
A and E are valid argument sysntax
QUE.6
Given
1. enum Animal {
2. DOG("woof"), CAT("meow"), FISH("burble");
3. String sound;
4. Animals(String s) { sound = s; }
5. }
6. class TestEnum {
7. static Animal a;
8. public static void main(String[] args) {
9. System.out.println(a.DOG.sound + " " +a.FISH.sound);
10. }
11. }
What is the result?
A. woof burble
B. Multiple Compilation errors
C. Compilation failes due to error on line 2
D. COmpilation failes due to error on line 3
E. COmpilation failes due to error on line 4
F. COmpilation failes due to error on line 9
Ans. A is correct;enums can have constructers and variables.
----------------------------------------------------------------------------------------------
QUE.7
Given two files
1. package pkgA;
2. public class Foo {
3. int a;
4. protected int b = 6;
5. public int c = 7;
6. }
3. package pkgB;
4. import pkgA.*;
5. public class Bar {
6. public static void main(String[] args) {
7. Foo f = new Foo();
8. System.out.println(" " + f.a);
9. System.out.println(" " + f.b);
10. System.out.println(" " + f.c);
11. }
12. }
What is the Result ?(Choose all that apply.)
A. 5 6 7
B. 5 followde by an Exception
C. Compilation failes with an error on line 7
D. Compilation failes with an error on line 8
E. Compilation failes with an error on line 9
F. Compilation failes with an error on line 10
Ans.
D and E are correct. Variable a has default access, so it cannot be accessed from outside the
package. Variable b has protected access in pkgA.
--------------------------------------------------------------------------------
QUE.8
Given
1. public class Electronic implements Device
{ public void doit() { } }
2.
3. abstract class Phone1
4.
5. abstract class Phone2 extends Electronics
{ public void doit(int x) { } }}
6.
7. class Phone3 extends Electronic implements Device
{public void doStuff( ) { } }
8.
9. interface Device { public void doit(); }
What is the result? ()Choose all that apply.)
A. Compilation succeeds
B. Compilation failes with an error on line 1
C.Compilation failes with an error on line 3
D. Compilation failes with an error on line 5
E. Compilation failes with an error on line 7
F. Compilation failes with an error on line 9
-----------------------------------------------------------------------------
QUE.9
Given
4. class Announce {
5. public static void main(String[] args) {
6. for(int__x=0;__x<3;__x++) ;
7. int #lb = 7;
8. long [] x [5];
9. Boolean []ba[];
10. enum Traffic { RED, YELLOW, GREEN };
11. }
12. }
-----------------------------------------------------------------------------
Given
4. class Announce {
5. public static void main(String[] args) {
6. for(int__x=0;__x<3;__x++) ;
7. int #lb = 7;
8. long [] x [5];
9. Boolean []ba[];
10. enum Traffic { RED, YELLOW, GREEN };
11. }
12. }
What is the result? (Choose all that apply.)
A. Compilation succeeds
B.Compilation fails with an error on line 6
C. Compilation fails with an error on line 7
D.Compilation fails with an error on line 8
E.Compilation fails with an error on line 9
F.Compilation fails with an error on line 10
Answer:
C, D, and F are correct. Variable names cannot begin with a #, an array declaration can’t
include a size without an instantiation, and enums can’t be declared within a method.
-----------------------------------------------------------------------------
QUE.10
Given
3. public class TestDays {
4. public enum Days { MON, TUE, WED}
5. public static void main(String[] args)
6. for (Days d : Days.values() )
7. ;
8. Days [] d2 = Days.values();
9. System.out.println(d2[2]);
10. }
11. }
What is the result? (Choose all that apply.)
Given
3. public class TestDays {
4. public enum Days { MON, TUE, WED}
5. public static void main(String[] args)
6. for (Days d : Days.values() )
7. ;
8. Days [] d2 = Days.values();
9. System.out.println(d2[2]);
10. }
11. }
What is the result? (Choose all that apply.)
A. TUE
B. WED
C. The output is unpredictable
D. Compilation fails due to an error on line 4
E. Compilation fails due to an error on line 6
of the enum's values, in the order in which they are declared in the enum.
-----------------------------------------------------------------------------
QUE.11
Given.
12. }
C.Compilation fails due to an error on line 6 D. Compilation fails due to an error on line 7
--------------------------------------------------------------------------------------------------------
F. Compilation fails due to an error on line 8
G.Compilation fails due to an error on line 9
Answer:
B is correct. Every enum comes with a static values() method that returns an arrayof the enum's values, in the order in which they are declared in the enum.
-----------------------------------------------------------------------------
QUE.11
Given.
4. public class Frodo extends Hobbit {
5. public static void main(String[] args) {
6. Short myGold = 7;
7. System.out.println(countGold(myGold, 6));
8. }
9. }
10. class Hobbit {
11. int countGold(int x, int y) { return x + y; }
12. }
What is the result?
A.13B. Compilation fails due to multiple errorsC.Compilation fails due to an error on line 6 D. Compilation fails due to an error on line 7
E. Compilation fails due to an error on line 11
Answer:
D is correct. The Short myGold is autoboxed correctly, but the countGold() method
cannot be invoked from a static context.
--------------------------------------------------------------------------------------------------------
QUE.12
Which is a correct class? (Choose all that apply.)
A. public abstract class Frob implements Frobnicate {
}
}
twiddle () method.
Given:
public abstract interface Frobnicate { public void twiddle(String s); }Which is a correct class? (Choose all that apply.)
A. public abstract class Frob implements Frobnicate {
public abstract void twiddle(String s) { }
}
B. public abstract class Frob implements Frobnicate { }
C.public class Frob extends Frobnicate {
public void twiddle(Integer i) { }
}
D.public class Frob implements Frobnicate {
public void twiddle(Integer i) { }
}
E.public class Frob implements Frobnicate {
public void twiddle(String i) { }
public void twiddle(Integer s) { }
}
Answer:
B is correct, an abstract class need not implement any or all of an interface’s methods.
E is correct, the class implements the interface method and additionally overloads the
----------------------------------------------------------------------------------
useful material
ReplyDelete