Social Icons

Featured Posts

RSS

Pages

Check Your Java Basics III

This is the third article of first and second quizzes. This is also containing 25 java questions with answers.


QUESTION: 51
Given:


 public class Threads2 implements Runnable {
           public void run() {
                 System.out.println("run.");
                 throw new RuntimeException("Problem");
           }
           public static void main(String[] args) {
                  Thread t = new Thread(new Threads2());
                  t.start();
                  System.out.println("End of method.");
           }
 }

Which two can be results? (Choose two.)
A.  java.lang.RuntimeException: Problem
B.  run.
      java.lang.RuntimeException: Problem
C. End of method.
     java.lang.RuntimeException: Problem
D. End of method.
     run.
     java.lang.RuntimeException: Problem
E. run.
     java.lang.RuntimeException: Problem
     End of method.

Answer: D, E


QUESTION: 52
Which two statements are true? (Choose two.)

A. It is possible for more than two threads to deadlock at once.
B. The JVM implementation guarantees that multiple threads cannot enter into a
     deadlocked state.
C. Deadlocked threads release once their sleep() method's sleep duration has expired.
D. Deadlocking can occur only when the wait(), notify(), and notifyAll() methods are
     used incorrectly.
E. It is possible for a single-threaded application to deadlock if synchronized blocks are
     used incorrectly.
F. If a piece of code is capable of deadlocking, you cannot eliminate the possibility of
    deadlocking by inserting invocations of Thread.yield().

Answer: A, F


QUESTION: 53
Given:

void waitForSignal() {
       Object obj = new Object();
       synchronized (Thread.currentThread()) {
       obj.wait();
       obj.notify();
      }
 }

Which statement is true?
A. This code can throw an InterruptedException. 
B. This code can throw an IllegalMonitorStateException.
C. This code can throw a TimeoutException after ten minutes.
D. Reversing the order of obj.wait() and obj.notify() might cause this method to complete
     normally.
E. A call to notify() or notifyAll() from another thread might cause this method to
     complete normally.
F. This code does NOT compile unless "obj.wait()" is replaced with "((Thread) obj).wait()".

Answer: B


QUESTION: 54
Given:
 class PingPong2 {
       synchronized void hit(long n) {
              for(int i = 1; i < 3; i++)
                 System.out.print(n + "-" + i + " ");
             }
       }
 public class Tester implements Runnable {
           static PingPong2 pp2 = new PingPong2();
           public static void main(String[] args) {
               new Thread(new Tester()).start();
               new Thread(new Tester()).start();
       }
 public void run() { pp2.hit(Thread.currentThread().getId()); }
}

Which statement is true?
A. The output could be 5-1 6-1 6-2 5-2
B. The output could be 6-1 6-2 5-1 5-2
C. The output could be 6-1 5-2 6-2 5-1
D. The output could be 6-1 6-2 5-1 7-1

Answer: B


QUESTION: 55
Given:

 public class Threads4 {
          public static void main (String[] args) {
                   new Threads4().go();
          }
          public void go() {
                  Runnable r = new Runnable() {
                       public void run() {
                              System.out.print("foo");
                  }
  };

    Thread t = new Thread(r);
     t.start();
     t.start();
      }
 }

What is the result?
A. Compilation fails.
B. An exception is thrown at runtime.
C. The code executes normally and prints "foo".
D. The code executes normally, but nothing is printed.

Answer: B


QUESTION: 56
Given:

 public abstract class Shape {
         private int x;
         private int y;
         public abstract void draw();
         public void setAnchor(int x, int y) {
         this.x = x;
         this.y = y;
     }
 }
Which two classes use the Shape class correctly? (Choose two.)
A. public class Circle implements Shape {
          private int radius;
    }
B. public abstract class Circle extends Shape {
         private int radius;
    }
C. public class Circle extends Shape {
        private int radius;
        public void draw();
   }
D. public abstract class Circle implements Shape {
        private int radius;
        public void draw();
   }

E. public class Circle extends Shape {
        private int radius;
        public void draw() {/* code here */}
F. public abstract class Circle implements Shape {
        private int radius;
        public void draw() { /* code here */ }

Answer: B,E


QUESTION: 57
Given:
 public class Barn {
         public static void main(String[] args) {
              new Barn().go("hi", 1);
              new Barn().go("hi", "world", 2);
        }
        public void go(String... y, int x) {
             System.out.print(y[y.length - 1] + " ");
       }
 }

What is the result?
A. hi hi
B. hi world
C. world world
D. Compilation fails.
E. An exception is thrown at runtime.

Answer: D


QUESTION: 58
Given:
 class Nav{
        public enum Direction { NORTH, SOUTH, EAST, WEST }
 }
 public class Sprite{
 // insert code here
 }

Which code, inserted at line 14, allows the Sprite class to compile?
A. Direction d = NORTH;
B. Nav.Direction d = NORTH;
C. Direction d = Direction.NORTH;
D. Nav.Direction d = Nav.Direction.NORTH;

Answer: D


QUESTION: 59
Given:
 public class Rainbow {
       public enum MyColor {
               RED(0xff0000), GREEN(0x00ff00), BLUE(0x0000ff);
               private final int rgb;
               MyColor(int rgb) { this.rgb = rgb; }
               public int getRGB() { return rgb; }
       };
   public static void main(String[] args) {
            // insert code here
     }
 }

Which code fragment, inserted at line 19, allows the Rainbow class to compile?
A. MyColor skyColor = BLUE;
B. MyColor treeColor = MyColor.GREEN;
C. if(RED.getRGB() < BLUE.getRGB()) { }
D. Compilation fails due to other error(s) in the code.
E. MyColor purple = new MyColor(0xff00ff);
F. MyColor purple = MyColor.BLUE + MyColor.RED;

Answer: B


QUESTION: 60
Given:

class Mud {
      // insert code here
     System.out.println("hi");
      }
 }
And the following five fragments:
public static void main(String...a) {
public static void main(String.* a) {
public static void main(String... a) {
public static void main(String[]... a) {
public static void main(String...[] a) {
How many of the code fragments, inserted independently at line 12, compile?


A. 0
B. 1
C. 2
D. 3
E. 4
F. 5

Answer: D


QUESTION: 61
Given:
 class Atom {
       Atom() { System.out.print("atom "); }
  }
  class Rock extends Atom {
       Rock(String type) { System.out.print(type); }
 }
 public class Mountain extends Rock {
       Mountain() {
       super("granite ");
       new Rock("granite ");
     }

 public static void main(String[] a) { new Mountain(); }
 }

What is the result?
A. Compilation fails.
B. atom granite
C. granite granite
D. atom granite granite
E. An exception is thrown at runtime.
F. atom granite atom granite

Answer: F



QUESTION: 62
Given:
1. interface TestA { String toString(); }
2. public class Test {
3. public static void main(String[] args) {
4. System.out.println(new TestA() {
5. public String toString() { return "test"; }

6. });
7. }
8. }

What is the result?
A. test
B. null
C. An exception is thrown at runtime.
D. Compilation fails because of an error in line 1.
E. Compilation fails because of an error in line 4.
F. Compilation fails because of an error in line 5.

Answer: A


QUESTION: 63
Given:
public static void parse(String str) {
   try {
        float f = Float.parseFloat(str);
    } catch (NumberFormatException nfe) {
            f = 0;
    } finally {
          System.out.println(f);
      }
 }
 public static void main(String[] args) {
         parse("invalid");
 }

What is the result?
A. 0.0
B. Compilation fails.
C. A ParseException is thrown by the parse method at runtime.
D. A NumberFormatException is thrown by the parse method at runtime.

Answer: B





QUESTION: 64
Given:
public class Blip {
      protected int blipvert(int x) { return 0; }
 }
 class Vert extends Blip {

 // insert code here
 }

Which five methods, inserted independently  will compile? (Choose five.)
A. public int blipvert(int x) { return 0; }
B. private int blipvert(int x) { return 0; }
C. private int blipvert(long x) { return 0; }
D. protected long blipvert(int x) { return 0; }
E. protected int blipvert(long x) { return 0; }
F. protected long blipvert(long x) { return 0; }
G. protected long blipvert(int x, int y) { return 0; }

Answer: A,C,E,F,G


QUESTION: 65
Given:
1. class Super {
2. private int a;
3. protected Super(int a) { this.a = a; }
4. }
...
11. class Sub extends Super {
12. public Sub(int a) { super(a); }
13. public Sub() { this.a = 5; }
14. }


Which two, independently, will allow Sub to compile? (Choose two.)
A. Change line 2 to:
     public int a;
B. Change line 2 to:
     protected int a;
C. Change line 13 to:
     public Sub() { this(5); }
D. Change line 13 to:
     public Sub() { super(5); }
E. Change line 13 to:
     public Sub() { super(a); }

Answer: C,D


QUESTION: 66
Which Man class properly represents the relationship "Man has a best friend who is a
Dog"?


A. class Man extends Dog { }
B. class Man implements Dog { }
C. class Man { private BestFriend dog; }
D. class Man { private Dog bestFriend; }
E. class Man { private Dog<bestFriend>; }
F. class Man { private BestFriend<dog>; }

Answer: D


QUESTION: 67
Given:
 package test;
 class Target {
        public String name = "hello";
 }

What can directly access and change the value of the variable name?
A. any class
B. only the Target class
C. any class in the test package
D. any class that extends Target

Answer: C


QUESTION: 68
Given:
 abstract class Vehicle { public int speed() { return 0; }
 class Car extends Vehicle { public int speed() { return 60; }
 class RaceCar extends Car { public int speed() { return 150; }
...
 RaceCar racer = new RaceCar();
 Car car = new RaceCar();
 Vehicle vehicle = new RaceCar();
 System.out.println(racer.speed() + ", " + car.speed() + ", " + vehicle.speed());

What is the result?
A. 0, 0, 0
B. 150, 60, 0
C. Compilation fails.
D. 150, 150, 150

E. An exception is thrown at runtime.

Answer: D


QUESTION: 69
Given:

5. class Building { }
6. public class Barn extends Building {
7. public static void main(String[] args) {
8. Building build1 = new Building();
9. Barn barn1 = new Barn();
10. Barn barn2 = (Barn) build1;
11. Object obj1 = (Object) build1;
12. String str1 = (String) build1;
13. Building build2 = (Building) barn1;
14. }
15. }

Which is true?
A. If line 10 is removed, the compilation succeeds.
B. If line 11 is removed, the compilation succeeds.
C. If line 12 is removed, the compilation succeeds.
D. If line 13 is removed, the compilation succeeds.
E. More than one line must be removed for compilation to succeed.

Answer: C


QUESTION: 70

A team of programmers is reviewing a proposed API for a new utility class. After some
discussion, they realize that they can reduce the number of methods in the API without
losing any functionality. If they implement the new design, which two OO principles will
they be promoting?

A. Looser coupling
B. Tighter coupling
C. Lower cohesion
D. Higher cohesion
E. Weaker encapsulation
F. Stronger encapsulation

Answer: A


QUESTION: 71
Given:

21. class Money {
22. private String country = "Canada";
23. public String getC() { return country; }
24. }
25. class Yen extends Money {
26. public String getC() { return super.country; }
27. }
28. public class Euro extends Money {
29. public String getC(int x) { return super.getC(); }
30. public static void main(String[] args) {
31. System.out.print(new Yen().getC() + " " + new Euro().getC());
32. }
33. }

What is the result?
A. Canada
B. null Canada
C. Canada null
D. Canada Canada
E. Compilation fails due to an error on line 26.
F. Compilation fails due to an error on line 29.

Answer: E


QUESTION: 72

Assuming that the serializeBanana() and the deserializeBanana() methods will correctly
use Java serialization and given:

 import java.io.*;
 class Food implements Serializable {int good = 3;}
 class Fruit extends Food {int juice = 5;}
 public class Banana extends Fruit {
       int yellow = 4;
       public static void main(String [] args) {
              Banana b = new Banana(); Banana b2 = new Banana();
              b.serializeBanana(b); // assume correct serialization
              b2 = b.deserializeBanana(); // assume correct
              System.out.println("restore "+b2.yellow+ b2.juice+b2.good);
        }
             // more Banana methods go here
 }
What is the result?


A. restore 400
B. restore 403
C. restore 453
D. Compilation fails.
E. An exception is thrown at runtime.

Answer: C


QUESTION: 73
Given a valid DateFormat object named df, and
 Date d = new Date(0L);
 String ds = "December 15, 2004";
 // insert code here

What updates d's value with the date represented by ds?

A.  d = df.parse(ds);
B.  d = df.getDate(ds);
C.  try {
      d = df.parse(ds);
      } catch(ParseException e) { };
D.  try {
     d = df.getDate(ds);
     } catch(ParseException e) { };

Answer: C


QUESTION: 74

Given:
 double input = 314159.26;
 NumberFormat nf = NumberFormat.getInstance(Locale.ITALIAN);
String b;
 //insert code here

Which code, inserted that sets the value of b to 314.159,26?

A. b = nf.parse( input );
B. b = nf.format( input );
C. b = nf.equals( input );
D. b = nf.parseObject( input );

Answer: B

QUESTION: 75


Given:
 public class TestString1 {
     public static void main(String[] args) {
          String str = "420";
          str += 42;
          System.out.print(str);
          }
 }

What is the output?
A. 42
B. 420
C. 462
D. 42042
E. Compilation fails.
F. An exception is thrown at runtime.

Answer: D

This brings end to this article. Thank You. 






























  • Digg
  • Del.icio.us
  • StumbleUpon
  • Reddit
  • RSS

0 comments:

Post a Comment

Who Is Online Now