Sie sind auf Seite 1von 30

Advisor adviser 1. Which is true? 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 a class and Y is a class C. "X extends Y" is correct if X and Y are both classes or both interfaces D. "X extends Y" is correct if all combinations of X and Y being classes and/or interfaces Ans: C Hint: classes implements interfaces, they don't extend them Interfaces only "inherit from" other interfaces 2. Which identifier names follow java standard? int .a; int 7g; int s#; int __________________2_q; Ans: D Hint: Identifier can start with letter ,currency character $ or connecting character ( _ ) 3. public class greeterbean { public greeterbean() { } public String greetme(String s) {

return "How are you?...."+s; } } A. compilation error B.

4. public class Test {


public static void main (String [] args) { String foo = "blue"; String bar = foo; foo = "green"; System.out.println(bar); } } public class second { public void printMethod() { System.out.println("Test Method"); } } A.blue B.Test Method C. blue Test Method D. Compiler error Ans. D

Expl:
A source code file can have only one public class. 5.
package cert; package exam; public class Beverage { public static void main (String [] args) {}

A. Compilation fails because no statement in main function B. Compilation fails because A file can have only one package statement C. Compilation fails because JavaBeans methods must be named using camelCase
D. Successfully compiled

Ans: B
A file can have only one package statement, but multiple imports. 6.
package cert; public class Beverage { public static void main (String [] args) {} } import cert.Beverage; package exam; public class Tea extends Beverage{ public static void main (String [] args) {} } A. Successful compilation B. Compiler error

The package statement (if any) must be the first (non-comment) line in a source file. The import statements (if any) must come after the package and before the class declaration.
7. 1. final class A { 2. public String toString () { 3. return "4"; 4. } 5. } 6. class B extends A { 7. public String toString () { 8. return super.toString() + "3"; 9. } 10.} 11.public class Test { 12.public static void main(String[]args) { 13.System.out.println(new B()); 14.} 15.}

a. 43

b. c. d. e.

Multiple compilation errors Compilation fails due to an error on line 1 Compilation fails due to an error on line 8 Compilation fails due to an error on line 6
Ans e

A final class cannot be subclassed.


7. 1. abstract class SuperClassA{ 2. public String toString () { 3. return "4";}} 4. 5. class SubClassB extends SuperClassA { 6. public String toString () { 7. SuperClassA ObjectForClassA = new SuperClassA(); 8. return super.toString() + "3";}} 9. public class Test { 10. public static void main(String[]args) { 11.System.out.println(new SubClassB());}}

a. 43

b. c. d. e. Ans d

Multiple compilation errors Compilation fails due to an error on line 1 Compilation fails due to an error on line 7 Compilation fails due to an error on line 6

An abstract class cannot be instantiated


8. 1. abstract class SuperClassA{ 2. public String toString () { 3. SuperClassA ObjectForClassA = new SuperClassA(); 4. return "4";}} 5. class SubClassB extends SuperClassA { 6. public String toString () { 7. return super.toString() + "3";}} 8. 9. public class Test { 10. public static void main(String[]args) { 11.System.out.println(new SubClassB());}}

43

Multiple compilation errors

Ans d

Compilation fails due to an error on line 1 Compilation fails due to an error on line 3 Compilation fails due to an error on line 7

An abstract class cannot be instantiated 9.


1. public class enclosingone ( 2. public class insideone{} 3. ) 4. public class inertest( 5. public static void main (string[]args)( 6. enclosingone eo= new enclosingone (); 7. //insert code here 8. ) 9. ) Which statement at line 7 constructs an instance of the inner class?. InsideOnew ei= eo.new InsideOn(); Eo.InsideOne ei = eo.new InsideOne(); InsideOne ei = EnclosingOne.new InsideOne(); EnclosingOne.InsideOne ei = eo.new InsideOne(); Ans: D

For a nested class , object for inner class can be instantiated by OuterClass.InnerClass NewObject = ObjectOuterClass.new InnerClass(); 10.
1. abstract class SuperClassA{ 2. public String toString () { 3. return "4"; } 4. abstract public void add(); 5. abstract public void sub(){} 6. } 7. class SubClassB extends SuperClassA { 8. public String toString () { 9. return super.toString() + "7"; 10.} 11. } 12. public class Test { 13. public static void main(String[]args) { 14. System.out.println(new SubClassB()); }} a. 47

b. Multiple compilation errors c. Compilation fails due to an error on line 4

d. Compilation fails due to an error on line 5 e. Compilation fails due to an error on line 7 Ans a The first concrete class to extend an abstract class must implement all of its abstract methods. Abstract method should not have function body 11. interface check { public void message(){} } class Interface { public static void main(String[] args) { try { check t = new check() { public void message() { System.out.println("Method defined in the interface"); } }; t.message(); } catch (Exception ex) { System.out.println("" + ex.getMessage()); } } } A. Compiler error:Interface method cannot have a body B. Method defined in the interface C. Compilation Successful but does not display any output Ans A An interface is like a 100-percent abstract class, and is implicitly abstract whether you type the abstract modifier in the declaration or not. Hence the method should be generic. 12. What is the result of compiling and running the following code?

public class Tester { static int p = test(); //line 1 static public int test() { System.out.print(p); //line 4

return 99; } public static void main(String[] args) { System.out.print(p);//line 11 } } Please choose only one answer:

099 Compilation error at line 1, p must be initialized by a value Compilation error at line 4, using uninitialized variable p Compilation error at line 11, p must be called using its class by writing Tester.p Ans A Explanation: In the main function, it tries to print p which is an object for the method test.In the method it prints p(the default value is printed, Default value for static variable is zero.) and returns 99 which is printed in the main function. 13.
package Test; interface foo { int k = 0;//line 3 } package Test; public class test implements foo { public static void main(String args[]) { int i; test t = new test (); i= t.k;//line 12 i= test.k;//line 13 i= foo.k;//line 14

} }

What is the result? A. Compilation succeeds. B. An error at line 3 causes compilation to fail. C. An error at line 12 causes compilation to fail. D. An error at line 13 causes compilation to fail. E. An error at line 14 causes compilation to fail. Ans: A

Explanation: Static variable in the interface can be accessed by using class name, interface name or object for the class.

14.

interface foo implements test //line 1 { int i; test t = new test (); i= t.k; //line 5 i= test.k; i= foo.k; //line 7 } public class test { public static void main(String args[]) { int k = 0; //line 12 } } What is the result? A. Compilation succeeds. B. An error at line 1 causes compilation to fail. C. An error at line 12 causes compilation to fail. D. An error at line 5 causes compilation to fail. E. An error at line 7 causes compilation to fail. Ans: B Explanation:

Interfaces cannot extend a class, or implement a class or interface 15 interface foo extends test //line 1 { int i; test t = new test (); i= t.k; //line 5 i= test.k; i= foo.k; //line 7 }
public class test { public static void main(String args[]) { int k = 0; //line 12 } } What is the result? A. Compilation succeeds. B. An error at line 1 causes compilation to fail. C. An error at line 12 causes compilation to fail.

D. An error at line 5 causes compilation to fail. E. An error at line 7 causes compilation to fail. Ans: B Explanation:

Interfaces cannot extend a class, or implement a class or interface 16. Given:


1. 2. 3. 4. 5. 6. 7. class Voop { public static void main(String [] args) { doStuff(1); doStuff(1,2); } // insert code here }

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) { } Answer:
static void doStuff(int... doArgs) { }

use valid var-args syntax.


static void doStuff(int[] doArgs) { } and static void doStuff(int doArgs...) { } are invalid var-arg syntax, and static void doStuff(int... doArgs, int y) { }

is invalid because the var-arg must be the last of a method's arguments. 17.
public class HelloWorldVarargs { public static void main(String args[]) { test(215, "India", "Delhi"); test(147, "United States", "New York", "California"); }
// insert code here

{ System.out.print("\n" + some); for(String arg: args) { System.out.print(", " + arg); } } }

Which, inserted independently at line 6, will compile? (Choose all that apply.)

A. public static void test(int some, String... args) B. public static void test(int some, String args...)

C. public static void test(String... args, int... some) D. public static void test(String... args, int some)
Ans: A Explanation: The var-arg must be the last parameter in the method's signature, and you can have only one var-arg in a method. 18.

Given:
1. enum Animals { 2. DOG("woof"), CAT("meow"), FISH("burble"); 3. String sound; 4. Animals(String s) { sound = s; } 5. } 6. class TestEnum { 7. static Animals 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 fails due to an error on line 2 D. Compilation fails due to an error on line 3 E. Compilation fails due to an error on line 4 F. Compilation fails due to an error on line 9 Answer: A is correct; enums can have constructors and variables. B, C, D, E, and F are incorrect; these lines all use correct syntax. (Objective 1.3) 19. Which of the following code is incorrect A public enum Color { WHITE, BLACK, RED, YELLOW, BLUE; }

public class Outter { public enum Color { WHITE, BLACK, RED, YELLOW, BLUE } } C public enum Color { WHITE, BLACK, RED, YELLOW, BLUE

@Override public String toString() { //only capitalize the first letter String s = super.toString(); return s.substring(0, 1) + s.substring(1).toLowerCase(); }

D
public enum Color { WHITE(21), BLACK(22), RED(23), YELLOW(24), BLUE(25); private int code; private Color(int c) { code = c; } public int getCode() { return code; } public enum Color implements Runnable { WHITE, BLACK, RED, YELLOW, BLUE; public void run() { System.out.println("name()=" + name() + ", toString()=" + toString()); } }

A. B. C. D.

C &D are incorrect A&B D alone B alone

Ans. C &D are incorrect ; is mandatory in option C And enum cannot be declared as private 20
public class EnumTest { Day day; public EnumTest(Day day) { this.day = day; } public void tellItLikeItIs() { switch (day) { case MONDAY:

System.out.println("Mondays are bad."); break; case FRIDAY: System.out.println("Fridays are better."); break; case SATURDAY: case SUNDAY: System.out.println("Weekends are best."); break; default: System.out.println("Midweek days are so-so."); break; } }

public static void main(String[] args) { EnumTest firstDay = new EnumTest(Day.MONDAY); firstDay.tellItLikeItIs(); EnumTest thirdDay = new EnumTest(Day.WEDNESDAY); thirdDay.tellItLikeItIs(); EnumTest fifthDay = new EnumTest(Day.FRIDAY); fifthDay.tellItLikeItIs(); EnumTest sixthDay = new EnumTest(Day.SATURDAY); sixthDay.tellItLikeItIs(); EnumTest seventhDay = new EnumTest(Day.SUNDAY); seventhDay.tellItLikeItIs(); }

A. The output is:


Mondays are bad. Midweek days are so-so. Fridays are better. Weekends are best. Weekends are best.

B.Compilation error 21. Given two files:


1. package pkgA; 2. public class Foo { 3. int a = 5; 4. protected int b = 6; 5. public int c = 7; 6. } 7. package pkgB; 8. import pkgA.*; 9. public class Baz { 10. public static void main(String[] args) {

11. Foo f = new Foo(); 12. System.out.print(" " + f.a); 13. System.out.print(" " + f.b); 14.System.out.print(" " + f.c); 15. } 16. }

What is the result? (Choose all that apply.) A. 5 6 7 B. 5 followed by an exception C. Compilation fails with an error on line 12 and 11 D. Compilation fails with an error on line 13 and 14 E. Compilation fails with an error on line 11 Answer: 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. A, B, C, and F are incorrect based on the above information. 22. Given two files:
1. package pkgA; 2. public class Foo { 3. int a = 5; 4. protected int b = 6; 5. public int c = 7; 6. } 7. package pkgA; 8. import pkgA.*; 9. public class Baz { 10. public static void main(String[] args) { 11. Foo f = new Foo(); 12. System.out.print(" " + f.a); 13. System.out.print(" " + f.b); 14.System.out.print(" " + f.c); 15. } 16. }

What is the result? (Choose all that apply.) A. 5 6 7 B. 5 followed by an exception C. Compilation fails with an error on line 12 D. Compilation fails with an error on line 13 E. Compilation fails with an error on line 14 F. Compilation fails with an error on line 11 Answer:
 CxaqhbCtwdbwdc `sc cde`xqwrd r a dv b`s ad `bbdvvdc hwghs wgd v`rd `bp`fd

23. Given two files:


1. 2. 3. 4. 5. package pkgA; class Foo { int a = 5; protected int b = 6; public int c = 7;

6. } 7. package pkgB; 8. import pkgA.*; 9. public class Baz { 10. public static void main(String[] args) { 11. Foo f = new Foo(); 12. System.out.print(" " + f.a); 13. System.out.print(" " + f.b); 14.System.out.print(" " + f.c); 15. } 16. }

What is the result? (Choose all that apply.) 567 5 followed by an exception Compilation fails with an error on line 12 Compilation fails with an error on line 13 Compilation fails with an error on line 14 Compilation fails with an error on line 11 and 8 Answer: F Foo method is not visible since it has default access. 24. Given two files:
1. package pkgA; 2. public class Foo { 3. private int a = 5; 4. protected int b = 6; 5. public int c = 7; 6. } 7. package pkgB; 8. import pkgA.*; 9. public class Baz extends Foo { 10. public static void main(String[] args) { 11. Foo f = new Foo(); 12. System.out.print(" " + f.a); 13. System.out.print(" " + f.b); 14.System.out.print(" " + f.c); 15. } 16. }

What is the result? (Choose all that apply.) 567 5 followed by an exception Compilation fails with an error on line 12 Compilation fails with an error on line 13 Compilation fails with an error on line 14 Answer: C f.a has Private access hence it is not visible to Baz 25.

Given:
1. public class Electronic implements Device { public void doIt() { } } 2. 3. abstract class Phone1 extends Electronic { } 4. 5. abstract class Phone2 extends Electronic { 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.) Compilation succeeds Compilation fails with an error on line 1 Compilation fails with an error on line 3 Compilation fails with an error on line 5 Compilation fails with an error on line 7 Compilation fails with an error on line 9 Answer: A is correct; all of these are legal declarations. B, C, D, E, and F are incorrect based on the above information.

26. Given:
1. public class Electronic extends Device { public void doIt() { } } 2. 3. abstract class Phone1 extends Electronic { } 4. 5. abstract class Phone2 extends Electronic { 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 fails with an error on line 1 C. Compilation fails with an error on line 3 D. Compilation fails with an error on line 5 E. Compilation fails with an error on line 7 F. Compilation fails with an error on line 9 Answer: B Class can not extend an interface can only implement. 27.

Given:
1. public abstract class Electronic implements Device { public void dontIt() { } } 2. 3. abstract class Phone1 extends Electronic { } 4. 5. abstract class Phone2 extends Electronic { 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 fails with an error on line 1 C. Compilation fails with an error on line 3 D. Compilation fails with an error on line 5 E. Compilation fails with an error on line 7 F. Compilation fails with an error on line 9 Answer: E An abstract implementing class does not have to implement the interface methods (but the first concrete subclass must). 28.
package pkgA; public abstract class Fruits implements aroma, Color{ } public interface aroma { int isAroma(); } public interface Color { String getColor(); }

A. B. C. D.

Cannot implement more than one interface Compilation succeeds Should implement all the methods in an interface Should implement method from atleast one interface

B. Can implement more than one interface An abstract implementing class does not have to implement the interface methods (but the first concrete subclass must). 29. public abstract class Fruits implements aroma, Color{
//line 1

} public interface aroma { int isAroma(); } public interface Color { String getColor(); } //line 5

public abstract class Lemon extends Fruits, Vegetables{ } public abstract class Vegetables { }

//line 8

//line 10

A. Compilation succeeds B. Compilation fails with an error on line 1 C. Compilation fails with an error on line 8 D. Compilation fails with an error on line 5 E. Compilation fails with an error on line 10 C A class Cannot extend more than one class 30. 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 cant include a size without an instantiation, and enums cant be declared within a method. A, B, and E are incorrect based on the above information. (Objective 1.3) 31. 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 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 array of the enum's values, in the order in which they are declared in the enum. 32. 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. 13 B. Compilation fails due to multiple errors C. 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. -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------33. Consider the following two classes declared and defined in two different packages, what can be added in class B to form what considered a correct access to class A from main() method of class B?

package subPackage; public class A { } package anotherPackage; // line 1 public class B { public static void main(String[] args) { // line 2

} } Please choose all the answers that apply:

I. at line 1 add nothing at line 2 add : new A(); II. at line 1 add: import package.*; at line 2 add : new subPackage.A(); III. at line 1 add: import subPackage.*; at line 2 add : new A(); IV. at line 1 add: import subPackage.A; at line 2 add : new A(); V. at line 1 add nothing at line 2 add : new subPackage.A();
Ans: III & IV To use a class from another package import statement must be used and import always appears immediately after package statement. Please choose all the answers that apply:

I. at line 1 add nothing at line 2 add : new A(); II. at line 1 add: import package.*; at line 2 add : new subPackage.A(); III. at line 1 add: import subPackage.*; at line 2 add : new A(); IV. at line 1 add: import subPackage.A; at line 2 add : new A(); V. at line 1 add nothing at line 2 add : new subPackage.A();
34. What is the result of compiling and running the following code? Please choose only one answer: public class Tester { static int x = 4; public Tester() { System.out.print(this.x); // line 4 Tester(); } public static void Tester() { // line 8 System.out.print(this.x); // line 9 } public static void main(String... args) { // line 12 new Tester(); }}

Compile error at line 4 (static x must be only accessed inside static methods ) Compile error at line 8 (constructors can't be static) Compile error at line 9 (static methods can't invoke this) Compile error at line 12 (invalid argument type for method main) 44 Ans: C Line 4 compiler error because static methods can't invoke this Line 8 compiler error because method name is same as constructor name Line 12 is a valid argument Hence compile error at Line 9 is correct. 35.
A top level Java class can have the following modifiers: Please choose all the answers that apply:

strictfp and final abstract and final public and abstract protected private Ans: A & C --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------36.
What is the output ?
package package2; class InitDemo{ static int i=demo(); static{System.out.println(i);} InitDemo(){ System.out.println("hello 1"); } public static void main(String... args){ System.out.println("Hello 2"); } static int demo(){ System.out.println("Inside Demo"); return 10; } }

Please choose only one answer:

1. Compilation error 2. IllegalArgumentException is thrown at runtime.

3. Inside Demo 10 Hello 2 4. Hello 2 Inside Demo 10 Ans. C Explanation: As soon as the class are loaded static variables are initialized first to initialize it demo must be called first then main method is called
37.

You have two packages, trunk1 and trunk2 where class Sheet declared in trunk1 and class Container declared in trunk2, the following code contains a compilation error, where could it be? package trunk1; public class Sheet { public static int pageNumber = 99; // line 1 Sheet() {} // line 2 } package trunk2; import trunk1.Sheet; public class Container { public static void main(String... args) { //line 1 System.out.print(Sheet.pageNumber); //line 2 Sheet sheet = new Sheet(); //line 3 } } Please choose only one answer:

In package trunk1 at line 2, constructor Sheet must be marked public like its class In package trunk2 at line 1, invalid string argument for method main() In package trunk2 at line 2, invalid access to member pageNumber In package trunk2 at line 3, invalid call to constructor Sheet() Ans:D Explanation: The Sheet constructor has a package access scope, so it cannot be called outside its package
38.

A top level Java class can have the following modifiers: Please choose all the answers that apply:

strictfp and final abstract and final public and abstract protected private
Ans: A & C Explanation: Top level Java class can use Public and implicit access modifiers, abstract and final structural modifiers and strictfp behavioural modifier But abstract and final can not be used together. 39.

An inner class in java can have the following modifiers (at the same time): Please choose all the answers that apply:

public and abstract abstract and final protected private Ans: A,C & D

Explanation: Class inside another class can use Public,Private,protected and implicit access modifiers, abstract,static and final structural modifiers and strictfp behavioural modifier But abstract and final can not be used together.

40. What does it mean to mark a calss strictfp? Please choose only one answer:

it means this class can't be subclassed it means this class can never be instantiated and it is to be extended it means that any method code in the class will conform to the IEEE 754 standard rules for floating points Ans: C

41. What is the result of compiling and running the following code?

class Base { private Base() {System.out.print("Base");} } public class Derived extends Base { public Derived() {System.out.print("Derived");} public static void main(String[] args) { new Derived(); } } Please choose only one answer:

BaseDerived Derived Exception is thrown at runtime Compilation error Ans:D


implicite super constructor Base is not visible, must explicitly invoke another constructor( it should not be private) 42. Which of the following is true? Please choose all the answers that apply:

When a class marked final, it cannot be extended When a method marked final, it cannot be overridden When a method marked final, it cannot be overloaded Local variables of a method cannot be final A variable of a primitive type (int,float,..) which marked final, its value at initialization cannot be changed Ans: A,B &E Final class cannot be extended, final method cannot be overridden since once we mark it as final it cannot be modified. Any variable marked as final cannot e changed. 43.
You have three packages, trunk1,trunk2 and trunk3, What is the expected output of compiling and running file Tester? package trunk1; public class Account { Account() { //line 1 System.out.println(&quot;Account&quot;);

} } package trunk2; import trunk1.Account; public class CurrentAccount extends Account{ public CurrentAccount() { // line 2 System.out.println(&quot;CurrentAccount&quot;); } } package trunk3; import trunk1.Account; import trunk2.CurrentAccount; // line 3 public class Tester { public static void main(String[] args) { Account c = new Account(); // line 4 CurrentAccount ca = new CurrentAccount(); } } Please choose all the answers that apply:

A. Account Account CurrentAccount B. Compilation error, in package trunk1 at line 1 C. Compilation error, in package trunk2 at line 2 D. Compilation error, in package trunk3 at line 3 E. Compilation error, in package trunk3 at line 4 Ans: C & E 44.
What is the result of compiling and running the following code? public class Tester { static int x = 4; int y = 9; public Tester() { System.out.print(this.x); // line 7 printVariables(); } public static void printVariables() { System.out.print(x); // line 12 System.out.print(y); // line 13 } public static void main(String... args) { // line 16 new Tester(); } } Please choose only one answer:

Compile error at line 7 (static x must be only accessed inside static methods ) Compile error at line 13 (static methods cant make reference to non-static variables ) Compile error at line 16 (invalid argument type for method main ) 49 Compile error at line 12 (must access x by writing Tester.x) Ans.A Static members cannot be accessed using this pointer. 50.
What can be marked with strictfp modifier? (choose three) Please choose all the answers that apply:

an interface a class a member method a member field a local variable Ans. A, B and C Explanation:
The strictfp modifier can be used in an interface declaration. The effect of the strictfp modifier is to make all float or double expressions within the interface declaration be explicitly FP-strict. The strictfp modifier can be used in Class and methods inside the class but not to variables. 51. Which of the following declaration will compile without errors? (choose two) Please choose all the answers that apply:

public abstract class Digit { public abstract void print(); } public class Digit { public abstract void print(); } public abstract class Digit { public abstract void print(){} } public abstract class Digit { public void print();} public class Digit { public void print(){};}

Ans: A & E Explanation:


public class Digit { public abstract void print(); } Should be an abstract class to define an abstract method

public abstract class Digit { public abstract void print(){} }

abstract method do not specify a body

public abstract class Digit { public void print();}


52.

his method requires body instead of semicolon

Considering the following declaration for interface Convertable, which of the following code segments will compile? public interface Convertable { int convertToInt(); char convertToChar(); } Choose the one that will compile A
class Digit implements Convertable { int convertToInt() { return 0; } char convertToChar() { return 0; } } B abstract class Digit implements Convertable { public int convertToInt() { return 0; } char convertToChar(); } C. abstract class Digit implements Convertable { int convertToInt() ; char convertToChar(); } D. abstract class Digit implements Convertable { public int convertToInt() { return 0; } } Ans: D

Explanation: Cannot reduce the access scope of the methods in an interface by the implementing

class from public to implicit package in the methods convertToInt() or


convertToChar().

53.
Considering the following declaration for interface Convertable, which of the following code segments will compile? public interface Convertable { int convertToInt(); char convertToChar(); } Choose the one that will compile A
class Digit implements Convertable { int convertToInt() { return 0; } char convertToChar() { return 0; } } B abstract class Digit implements Convertable { public int convertToInt() { return 0; } char convertToChar(); } C. abstract class Digit implements Convertable { int convertToInt() ; char convertToChar(); } D. class Digit implements Convertable { public char convertToChar() { return 0; } public int convertToInt() { return 0; } } Ans: D

Explanation:

Cannot reduce the access scope of the methods in an interface by the implementing class from public to implicit package in the methods convertToInt() or
convertToChar().

54. 54.
Considering the following declaration for interface Convertable, which of the following code segments will compile? public interface Convertable { int convertToInt(); char convertToChar(); } Choose the one that will compile A
class Digit implements Convertable { int convertToInt() { return 0; } char convertToChar() { return 0; } } B abstract class Digit implements Convertable { public int convertToInt() { return 0; } char convertToChar(); } C. abstract class Digit implements Convertable { int convertToInt() ; char convertToChar(); } D. interface Roundable extends Convertable { int roundUp(); } Ans: D

Explanation:

Cannot reduce the access scope of the methods in an interface by the implementing class from public to implicit package in the methods convertToInt() or
convertToChar().

55.
Is the following declaration for interface Bendable correct and free of compilation error? Please choose only one answer: abstract interface Bendable { // line 1 final int x = 2009; // line 3 void method1() ; // line 5 public static class Angle {} // line 6 }

Yes, this is a correct and free of error declaration No, compilation error at line 1 , abstract should be removed No, compilation error at line 3 , x should be declared public final No, compilation error at line 5 , method method1() should be declared public abstract No, compilation error at line 6 , can't declare a class inside an interface Ans. A Explanation: An interface by default is abstract and public, nothing wrong in specifying it Any variable in an interface by default is final, static and public Methods in an interface by default is public and abstract Class can be declared inside an interface -------------------------------------------------------------------------------------56.
The following code contains a compilation error , what can be done to fix this error independently? abstract class AirPlane { // line 1 abstract void fly(); // line 2 void land() { System.out.print("Landing.."); } } class AirJet extends AirPlane { // line 10 AirJet() { super(); // line 13 } void fly() { System.out.print("Flying.."); } abstract void land() ; // line 20 }

Remove abstract from line 20 and add body to method land()

Declare class AirJet as abstract to at line 10 Remove super() call at line 13 Remove abstract at line 1 and line 2
Ans: A and B Explanation: Abstract method can be defined only in an abstract class

57.
Which of the following variables is incorrectly declared? public abstract interface Bouncable { int a = 0; public int b = 1; public static int c = 2; public static transient int d = 3; public final int e = 3; public static final int f = 3; } Please choose only one answer:

a b c d e f Ans d Explanation: Transient modifier is related to serialization. Transient means that the
field is not automatically serialized. Static fields are always transient. They can't be serialized because they don't belog to any instance.

Das könnte Ihnen auch gefallen