Вы находитесь на странице: 1из 17

1Q Given: 1. class GlsCls { 2. 3. } 4. 5. public class PlsCls extends GlsCls { 6. public PlsCls(int b) { 7. super(b); 8. } 9.

} Which GlsCls constructor must be inserted on line 2?

A B C D

public public public public

GlsCls() {} GlsCls(b) {} GlsCls(int b) {} GlsCls(String b) {}

2Q class test { 2. public int methodA() { 3. return 4; 4. } 5. } 6. public class testb extends test { 7. public static float methodA(int i) { 8. return 8.0f; 9. } 10. public static void main(String args[]) { 11. System.out.println("The answer is " + methodA(3)); 12. } 13. } What is the result?

A B C D

Compilation Compilation Compilation Compilation

fails because of fails because of succeeds and the succeeds and the

an error on line 7. an error on line 11. program prints "The answer is 4". program prints "The answer is 8.0".

3Q Given: 1. public class RstOr { 2. 3. return ((a * c)/(c + a)); 4. } 5. } What should be inserted on line 2 to allow compilation to succeed?

A B C D

int rstMth(float a, byte c) { long rstMth(float a, byte c) { byte rstMth(float a, byte c) { float rstMth(float a, byte c) {

4Q class GetIt { 2. public Double hereItIs() { 3. Double x = new Double(5.5); 4. return (x); 5. } 6. } 7. 8. public class TestIt extends GetIt { 9. public Integer hereItIs() { 10. Integer y = new Integer(10); 11. return (y); 12. } 13. 14. public static void main(String [] args) { 15. GetIt g = new GetIt(); 16. TestIt t = new TestIt(); 17. System.out.println(g.hereItIs() + " " + t.hereItIs()); 18. } 19. } A B C D Compilation Compilation Compilation Compilation fails. succeeds and the program prints "10". succeeds and the program prints "5.5". succeeds and the program prints "5.5 10".

5Q Given: 1. public class SeeIt { 2. ______ seeMth(char a, byte b) { 3. return (a + (short)b); 4. } 5. } What should be inserted in the blank on line 2 to allow compilation to succeed?

A int B byte C char D

short 6Q class MyPay { 2. public String getPay() { 3. return "Pay is 4.50"; 4. } 5. } 6. 7. public class MyTip extends MyPay { 8. public String getPay() { 9. return "Pay is 6.50"; 10. } 11. 12. public static void main(String [] args) { 13. MyTip mt = new MyTip(); 14. System.out.println(mt.getPay()); 15. } 16. } A Compilation fails. B Compilation succeeds and the program prints "Pay is 4.50". C Compilation succeeds and the program prints "Pay is 6.50". D Compilation succeeds, but an exception occurs during program execution. Which statement about subclass method return types is true?

7Q Given: 1. public class ClassA { 2. public static void main(String [] args) { 3. 4. switch(x) { 5. default: 6. System.out.println("Here it is."); 7. } 8. } 9. } The ClassA class can be compiled successfully by inserting one of three possible options on line 3. When inserted separately, which three will allow compilation to succeed? (Choose three.)

A int x = 6; B short x = 3; C char x = 'y';

D long x = 354; 8Q public class Tclass { 2. public static void main(String [] args) { 3. int x = 3; 4. int y = 4; 5. if ( ______ ) { 6. System.out.println("You didn't get it."); 7. } 8. else { 9. System.out.println("You didn't get it."); 10. } 11. } 12. } Which expression may be inserted in the blank on line 5 to allow compilation to succeed?

A x = y B x &= y C x == y D x && y 9Q public 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. } A A compilation error occurs on line 2. B A compilation error occurs on line 3. C A compilation error occurs on line 8. D Compilation succeeds and the program prints "No".

class MyClass { public static double getPay(float p) { return (p * 4.2); } public static void main(String [] args) { int y = 10; if ((getPay(2.2) <= y) (getPay(3.1) <= y)) System.out.println("Yes"); else System.out.println("No"); }

10Q public class SwitchIt { 2. public static void main(String[] args) { 3. int weeb = 1; 4. int woob = 2; 5. System.out.println(getWeebWoob(weeb, woob)); 6. } 7. 8. public static int getWeebWoob(int x, int y) { 9. switch (x) { 10. case 1: x = x + y; 11. case 2: x = x + y; 12. } 13. return x; 14. } 15. } A Compilation fails because of an error on line 9. B Compilation succeeds and the program prints "3". C Compilation succeeds and the program prints "5". D Compilation fails because of errors on lines 10 and 11. 11Q public class SpecTurn { 2. public static void main(String[] args) { 3. int p0 = 3; 4. int p1 = 3; 5. 6. 7. case(0): 8. System.out.println("I have it."); 9. break; 10. case(1): 11. System.out.println("You have it."); 12. break; 13. case(2): 14. System.out.println("We have it."); 15. break; 16. default: 17. System.out.println("They have it."); 18. } 19. } 20. } What should be inserted on line 6 to produce the output "They have it."?

A switch(p0 B switch(p0 ! p1) { C p1) {

switch(p0 D

p1) {

switch(p0 && p1) { 12Q public class PartPick { 2. public static void main(String[] args) { 3. System.out.println(getMthA(args[0])); 4. } 5. 6. public static String getMthA(String x) { 7. if(x) { 8. x += "'s turn"; 9. return x; 10. } 11. else { 12. return x; 13. } 14. } 15. } Given this command-line invocation line: java PartPick Rick What is the result?

A Compilation fails. B Compilation succeeds and the program prints "Rick". C Compilation succeeds and the program prints nothing. D Compilation succeeds and the program prints "Rick's turn". 13Q public class CamTest { 2. public static void main(String [] args) { 3. String[] y = new String[1]; 4. y[0] = args[0]; 5. String x = "hello"; 6. 7. System.out.println("match"); 8. } 9. else { 10. System.out.println("no match"); 11. } 12. } 13. } What should be inserted on line 6 to allow compilation to succeed?

A if (x & y[0]) {

B if (x.equals.y[0]) { C if (x = y[0].value) { D if (x.equals(y[0])) { 14Q public class SofPln { 2. public static void main(String[] args) { 3. int juju = 10; 4. int wuwu = 20; 5. 6. 7. System.out.println("juju" + "wuwu"); 8. } 9. else { 10. System.out.println("ju" + "wu"); 11. } 12. } 13. } Which code, when inserted on line 6, will cause a compilation error?

A if (juju == wuwu) { B if (juju <= wuwu) { C if (juju && wuwu) { D if (juju != wuwu) { 15Q Given: 1. public class MyTest { 2. public static void main(String args[]) { 3. int b = 0; 4. while (______) { 5. System.out.println("b is equal to " + b); 6. b++; 7. } 8. } 9. } What should be entered in the blank on line 4 to allow compilation to succeed?

A b(5) B b & 5 C b = 5

D b < 5 16Q public class ReClass { 2. public static void main(String [] args) { 3. int x = 0; 4. int y = 0; 5. int hold = 0; 6. System.out.println("This is a test."); 7. do { 8. hold = (--y + x++); 9. System.out.println("hold, y, x = " + hold + ", " + y + ", " + x); 10. } while (x <= 5); 11. } 12. } What is the result?

A Compilation fails because of an error on line 8. B Compilation fails because of an error on line 10. C Compilation succeeds and the last message printed is "hold, y, x = 0, -3, 3". D Compilation succeeds and the last message printed is "hold, y, x = -1, -6, 6". 17Q public class DagRag { 2. public static void main(String [] args) { 3. 4. int [][] x = new int[2][4]; 5. 6. for(int y = 0; y < 2; y++) { 7. for(int z = 0; z < 4; z++) { 8. x[y][z] = z; 9. } 10. } 11. 12. dg: for(int g = 0; g < 2; g++) { 13. rg: for(int h = 0; h < 4; h++) { 14. System.out.println(x[g][h]); 15. 16. } 17. System.out.println("The end."); 18. 19. } 20. 21. } 22. } To allow compilation to succeed, and the output to result in: 0 1 2

3 The end. What should be inserted on lines 15 and 18 respectively?

A if(h==3) break rg; if(g==0) break dg; B if(g==3) break rg; if(h==0) break dg; C if(h > 3) break dg; if(g > 0) break dg; D if(h > 3) break dg; if(g > 0) break rg;

18Q Given: 1. public class GrphIt { 2. public static void main(String[] args) { 3. char c = 'a'; 4. while(c = 'a') { 5. c += 'b'; 6. System.out.println(c); 7. } 8. } 9. } What is the result?

A Compilation fails because of an error on line 3. B Compilation fails because of an error on line 4. C Compilation succeeds and the program prints "a". D Compilation succeeds and the program prints "ab".

19Q public class DartCo { 2. public void getDart(int x) { 3. int cnt = 0; 4. do {

5. if (cnt == 0) { 6. System.out.println("Welcome!"); 7. } 8. else { 9. System.out.println(x); 10. x++; 11. } 12. cnt++; 13. } while (cnt < 15); 14. } 15. 16. public static void main(String [] args) { 17. DartCo dc = new DartCo(); 18. dc.getDart(25); 19. } 20. } What is the value of x at line 9 when the value of cnt equals 14?

A 10 B 25 C 34 D 38 20Q Given: 1. public class TinMan { 2. public static void main(String [] args) { 3. int j = 2, y = 3, z = 10; 4. for (;j < 6;j++) { 5. y = (++y + z); 6. System.out.println(y); 7. } 8. } 9. } What is the output produced by the program?

A: 13 23 33 43 B: 13 24 36 49 C:

14 25 36 47 D: 14 26 39 53 21Q public class FitLin { 2. public static void main(String [] args) { 3. int x = 1; 4. for(int y = 0; y < 4; y++) { 5. try { 6. if(x > 2) { 7. throw new Exception(); 8. } 9. System.out.println(x); 10. x++; 11. } 12. catch (Exception e) { 13. System.out.println("Exception"); 14. return; 15. } 16. } 17. System.out.println("The End"); 18. } 19. } What is the result?

A Compilation fails because of an error on line 7. B Compilation fails because of an error on line 12. C Compilation succeeds and the program prints: 1 2 Exception D Compilation succeeds and the program prints: 1 2 Exception The End 22Q public class CoProd { 2. static int i = 3; 3. public static void main(String [] args) { 4. try { 5. int c = coMeth();

6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. } What

if(c > 0) { throw new Exception(); } System.out.println(c); } catch (Exception e) { System.out.println("Got it."); System.exit(0); } finally { System.out.println("At last."); } } public static int coMeth() { i = i + 5; return i; } is the result?

A Compilation fails because of an error on line 13. B Compilation fails because of an error on lines 5 and 7. C Compilation succeeds and the program prints: Got it. D Compilation succeeds and the program prints: 8 At last. 23Q public class MyException { 2. public static void myMethA() throws Exception { 3. System.out.println("myMethA"); 4. } 5. public static void myMethB() { 6. System.out.println("myMethB"); 7. myMethA(); 8. } 9. public static void main(String [] args) { 10. try { 11. myMethB(); 12. } 13. catch (Exception e) { 14. System.out.println("exception received"); 15. } 16. } 17. } What is the result?

A Compilation fails because of an error on line 2. B Compilation fails because of an error on line 7. C Compilation succeeds and the program prints: myMethB myMethA D Compilation succeeds and the program prints: myMethA myMethB 24Q public class Gritch { 2. 3. throw new Exception(); 4. } 5. 6. public static void main(String [] args) { 7. Gritch g = new Gritch(); 8. try { 9. g.methoda(); 10. } 11. catch (Exception e) { 12. System.out.println("Caught exception."); 13. } 14. } 15. } What should be inserted on line 2 to allow compilation to succeed?

A public void methoda() { B public void methoda(Exception e) { C public void methoda() throws Exception { D public void methoda() extends Exception { 25Q public class Modge { 2. public static void main(String [] args) { 3. int[] x = {2,6,10}; 4. try { 5. for (int y=0; y<3; y++) { 6. System.out.println(x[y]); 7. } 8. } 9. catch (Exception e) { 10. System.out.println("Exception caught"); 11. } 12. finally { 13. System.out.println("Ending try");

14. } 15. } 16. } What is the result?

A Compilation fails. B Compilation succeeds and the program prints: Ending try C Compilation succeeds and the program prints: 2 6 10 Ending try D Compilation succeeds and the program prints: Exception caught Ending try 26Q What is the parent class for the Exception class? A java.lang.Error B java.lang.Throwable C java.lang.InternalError D java.lang.RuntimeException 27Q public class CofBrk { 2. public static void main(String [] args) { 3. try { 4. if (args[0].equals("")) { 5. throw new Exception(); 6. } 7. else { 8. System.out.println(args[0]); 9. } 10. } 11. catch (Exception e) { 12. System.out.println("Caught it"); 13. } 14. } 15. } After compiling the program, and executing it using this command-line invocation : java CofBrk Trigger What is the result?

A Compilation fails because of an error on line 3. B Compilation fails because of an error on line 11. C Compilation succeeds and the program prints "Trigger". D Compilation succeeds and the program prints "Caught it". 28Q public class Flip { 2. public static void main(String [] args) { 3. int a = Integer.parseInt(args[0]); 4. int b = Integer.parseInt(args[1]); 5. try { 6. int c = (a / b); 7. System.out.println(c); 8. } 9. catch (ArrayIndexOutOfBoundsException e1) { 10. System.out.println("You must enter two integer arguments."); 11. } 12. catch (Exception e) { 13. System.out.println("The second argument cannot be 0."); 14. } 15. finally { 16. System.out.println("Try it again!"); 17. } 18. } 19. } After compiling the program, and executing it using this command-line invocation : java Flip 50 What is the result?

A Compilation fails because of an error on line 9. B Compilation succeeds and the program prints: Try it again! C Compilation succeeds and the program prints: 25 Try it again! D Compilation succeeds, but an exception occurs during program execution. 29Q public class RemShw { 2. public static void main(String [] args) {

3. int x; 4. String y; 5. for (int z = 0; z < 4; z++) { 6. y = "paint"; 7. System.out.println(y + " " + x); 8. } 9. } 10. } What is the result?

A Compilation fails because of an error on line 6. B Compilation fails because of an error on line 7. C Compilation succeeds and the program prints "paint". D Compilation succeeds, but an exception occurs during program execution.

30Q public class Upgrd { 2. public void mth1() throws Exception { 3. throw new Exception(); 4. } 5. public void mth2() throws Exception { 6. mth1(); 7. } 8. public static void main(String [] args) { 9. Upgrd u = new Upgrd(); 10. try { 11. u.mth2(); 12. System.out.print("no error - "); 13. } 14. catch (Exception e) { 15. System.out.print("error - "); 16. } 17. finally { 18. System.out.println("finished"); 19. } 20. } 21. } What is the result?

A Compilation fails because of an error on line 3. B Compilation fails because of an error on line 5. C Compilation succeeds and the program prints:

finished D Compilation succeeds and the program prints: error - finished

Вам также может понравиться