Wednesday, 25 July 2012

java.lang.Thread - Objective Questions - Part 2

1)
Which of the following methods is not a member of the Object class?
  1. wait()
  2. notify()
  3. notifyAll()
  4. join()
Answer:join()

join() is a non-static method defined within the class java.lang.Thread.

2)
class A extends Thread {
  public void run() {
    try {sleep(10000);} catch (InterruptedException ie){}}
  public static void main(String[] args) {
    A a1 = new A();
    long startTime = System.currentTimeMillis();
    a1.start();
    System.out.print(System.currentTimeMillis() - startTime);
}}

What are the possible results of attempting to compile and run the program?
  1. Prints a number greater than or equal to 0
  2. The number printed must always be greater than 10000
  3. Compile-time error
  4. Run-time error

java.lang.Thread - Objective Questions - Part 1


1)
class Main implements Runnable {
    public void run() {
        System.out.println("thread");
    }

    public static void main(String args[]) {
        Main m = new Main();
        m.start();
    }
}
// What is the result of attempting to compile and run the program?

  1. Prints thread
  2. Prints Nothing
  3. Compile-time error
  4. Run-time error
Answer:Compile-time error.

start() method is declared with in java.lang.Thread class.So here to start the thread use following
Thread t=new Thread(m);
m.start();
It will start a new thread and prints the message 'thread'.

Operators and Assignments - Objective Questions - Part 2

1)

What will be the output of the program?

class Equals
{
    public static void main(String [] args)
    {
        int x = 100;
        double y = 100.1;
        boolean b = (x = y); /* Line 7 */
        System.out.println(b);
    }
}

A. true
B. false
C. compile-time error
D. Run-time error

Answer: C

The code will not compile because in line 7, the line will work only if we use (x==y) in the line. The == operator compares values to produce a boolean, whereas the = operator assigns a value to variables.
Option A, B, and D are incorrect because the code does not get as far as compiling. If we corrected this code,the output would be false.

Operators and Assignments - Objective Questions - Part 1

1)

What will be the output of the program?

class Test
{
    static int s;
    public static void main(String [] args)
    {
        Test p = new Test();
        p.start();
        System.out.println(s);
    }

    void start()
    {
        int x = 7;
        twice(x);
        System.out.print(x + " ");
    }

    void twice(int x)
    {
        x = x*2;
        s = x;
    }
}

A. 7 7
B. 7 14
C. 14 0
D. 14 14

Answer: B

Object-Oriented concepts - Objective Questions - Part 2

1)

class GFC500 {private String name;}
class GFC501 {
  private String name;
  private void setName(String name) {this.name = name;}
  private String getName() {return name;}
}
class GFC502 {
  private String name;
  public void setName(String name) {this.name = name;}
  public String getName() {return name;}
}

Which class is not tightly encapsulated?

A. GFC501
B. GFC502
C. GFC503
D. None of the above