Search This Blog

Monday, January 31, 2011

Question 22

What is the result of compiling and running the given code? (Choose 1 Answer)

import java.text.NumberFormat;
import java.text.ParseException;
import java.util.Locale;
public class NumberFormatClass {
  public static void main(String[] args){
    NumberFormat numberFormat = NumberFormat.getInstance(Locale.US);
    try{
      Number number = numberFormat.parse("4,455.67");
      System.out.println(number.intValue());
    }catch(ParseException ex){
      ex.printStackTrace();
    }
  }
}
Options :

a. Compiler error :intValue method not defined in Number
b. Prints "Error"
c. Prints 4,455
d. Prints 4455
e. Prints 4455.67


Question 22SocialTwist Tell-a-Friend

Sunday, January 30, 2011

Question 21

What is the result of attempting to compile and run the given code?

import java.util.*;
public class ScannerDelimiter2 {
  public static void main(String[] args) {
    String input = "10 Apples 20 Oranges 30 Pears";
    Scanner s = new Scanner(input).useDelimiter("\\d");
    String str[] = new String[6];
    int i=0;
    while(s.hasNext()){
      str[i]=s.next();
      System.out.println(str[i++]);
    }
  }
}
Options:


a. Prints "apples", "oranges", "pears" in separate lines.
b. Prints "10", "apples", "20", "oranges", "30", "pears" in separate lines.
c. Exception thrown at runtime.
d. Code does not compile.
e. None of these


Question 21SocialTwist Tell-a-Friend

Question 20

What is the result of compiling and running the given code?

import java.util.Scanner;
public class ScannerDelimiter {
  public static void main(String[] args){
    String input = "AA BB CC";
    Scanner s = new Scanner(input).useDelimiter("\s");
    while(s.hasNext()){
      System.out.print(s.next());
    }
  }
}
Options :


a. Prints "AA BB CC"
b. Prints "AABBCC"
c. Does not compile
d. Causes an exception to be thrown


Question 20SocialTwist Tell-a-Friend

Question 19

What is the result of compiling and running the given code?

public class NullPointerString {
  public static void main(String[] args){
    StringBuffer sb = new StringBuffer("Java");
    String nullString = null;
    sb.append(nullString);
    System.out.println(sb);
  }
}

Options :


A. Compiler error at line “String nullString = null”.
B. NullPointerException thrown at runtime.
C. Prints "Javanull".
d. None of the above.


Question 19SocialTwist Tell-a-Friend

Question 18

Which of the following methods are defined by the Serializable interface?

Options :

A. readObject() and writeObject()

B. readExternal() and writeExternal()

C. serialize() and deserialize()

D. None of the Above

Question 18SocialTwist Tell-a-Friend

Saturday, January 29, 2011

Question 17

Which code fragment produces the same output as the following code?

for(int i=0; i< 10; i++)
  System.console().printf("%d", i);
Options :

A. for(int i=0; i< 10; i++)
  System.console().print("%d", i);
B. for(int i=0; i< 10; i++)
  System.console().println("%d", i);
C. for(int i=0; i< 10; i++)
  System.console().write("%d", i);
D. for(int i=0; i< 10; i++)
  System.console().format("%d", i);

Question 17SocialTwist Tell-a-Friend

Question 16

Which of the following classes define the printf() method? Select two choices.

Options :

A. Console

B. PrintWriter

C. Formatter

D. Printer

Question 16SocialTwist Tell-a-Friend

Question 15

Which of the following are true about the readPassword() method of the Console class? Select two choices.

Options :

A. Its return type is String.

B. It reads the password from the console.

C. It has echoing disabled.

D. It takes a single argument.

E. None of these.

Question 15SocialTwist Tell-a-Friend

Friday, January 28, 2011

Question 14

What will be the result of attempting to compile and run the following program?

public class CustomThread extends Thread{
  public void run(){
    callMethod();
  }
  public synchronized void callMethod(){
    System.out.println(Thread.currentThread().getName());
  }
  public CustomThread(String name){
    super(name);
  }
  public static void main(String[] args){
    CustomThread a = new CustomThread("A");
    CustomThread b = new CustomThread("B");
    b.start();
    b.yield();
    a.run();    
  }
}
Options :


a. Prints "BA"
b. Prints "AB"
c. Prints "Bmain"
d. Prints "mainB"
e. Output can be "Bmain" or "mainB"
f. Output can be "BA" or "AB"
g. Code does not compile


Question 14SocialTwist Tell-a-Friend

Question 13

Before which of the following can the "synchronized" keyword be placed, without causing a compilation error?

Options :

A. Class Methods

B. Instance Methods.

C. Any block of code with in a method.

D. Variables.

E. A Class

Question 13SocialTwist Tell-a-Friend

Question 12

What would be the result of an attempt to compile and run the following code?

class TwoThreadsDemo extends Thread {
  public TwoThreadsDemo(String str){
    super(str);
  }
  public void run(){
    for(int i=0; i<10; i++){
      System.out.println(i+" "+getName());
      try{
        sleep((long)(Math.random()*1000));
      }catch(InterruptedException ie){
        ie.printStackTrace();
      }
      System.out.println("Done with Sleep !! "+getName());
    }
  }
}
public class SimpleThread{
  public static void main(String[] args){
    new TwoThreadsDemo("TwoThreadsDemo").start();
    new TwoThreadsDemo("TwoThreadsDemo").start();
  }
}
Options :


A: It will not compile because TwoThreadsDemo is a subclass of thread, but does not have a no-argument public constructor.
B. It will not compile because two separate threads in line A and line B have been given the same name "Java Programmer".
C. It will compile, but at runtime the second thread will overwrite the first thread and so the run method of the TwoThreadsDemo class will be executed only once instead of twice.
D. It will compile but will throw RuntimeException.
E It will compile and run with success and the run method of TwoThreadDemo class will be executed twice by two separate threads created.


Question 12SocialTwist Tell-a-Friend

Question 11

What would be the result of an attempt to compile and run the following code?
Choose one answer.

public class OrderedThread {
  public static void main(String[] args) {
    ThreadHelper first,second,third;
    OrderedThread ot = new OrderedThread();
    first = new ThreadHelper("one",ot);
    second = new ThreadHelper("second",ot);
    third = new ThreadHelper("third",ot);
    second.start();
    first.start();
    third.start();
  }
  public void display(String msg){
    synchronized(msg){
      for(int i=0;i<20;i++){
        System.out.println("Name= "+msg);
      }
    }
  }
}
class ThreadHelper extends Thread{
  String name;
  OrderedThread ot;
  ThreadHelper(String name, OrderedThread ot){
    this.name = name;
    this.ot = ot;
  }
  public void run(){
    ot.display(name);
  }
}
Options :

A .The code compiles fine and prints Two, One, and Three in order and repeats the same for another 19times.
B. The code compiles fine and prints Two 20 times, One 20 times, and Three 20 times sequentially. However, the order whether Two will come first, then One, and at last Three is not guaranteed.
C. The code compiles fine and prints Two 20 times, One 20 times, and Three 20 times sequentially at the behavior is guaranteed.
D. The code compiles fine and prints Two, One, and Three 20 times each in an indefinite order.
E. The code compiles fine but throws InterruptedException due to synchronization of msg String in a  wrong way.


Question 11SocialTwist Tell-a-Friend

Question 10

What will happen when you attempt to compile and run the following code?

public class TestThread extends Thread{  
  public static void main(String[] args) {
    new TestThread().start();
    new TestThread().start();
  }
  public void run(){
    SafeClass st = new SafeClass("abc");
    SafeClass st1 = new SafeClass("xyz");
  }
}
class SafeClass {
  private String str;
  public  SafeClass(String s){
    str = s;
    str.toUpperCase();
    System.out.println(str+" ");
    
  }
}
Options :


a. It will print - ABC XYZ ABC XYZ in that sequence.
b. It will print - ABC ABC XYZ XYZ in that sequence
c. It will print - ABC and XYZ twice, but the sequence cannot be determined.
d. It will print - abc and xyz twice, but the sequence cannot be determined.
e. The code will not compile.


Question 10SocialTwist Tell-a-Friend

Question 9

Which of the following statements are true about the sleep method of Thread class? Select two choices.

Options :

A . A thread releases any locks it is holding when it is invoked on the thread.

B. It is defined as static within the Thread class.

C. A Thread starts running as soon as it wakes up from the sleeping state.

D. The sleep method throws InterruptedException.

Question 9SocialTwist Tell-a-Friend

Question 8

What will happen when you attempt to compile and run the following code?

public class TestThread extends Thread{  
  public static void main(String[] args) {
    new TestThread().start();
    new TestThread().start();
  }
  public void run(){
    SafeClass st = new SafeClass("abc");
    SafeClass st1 = new SafeClass("xyz");
  }
}
class SafeClass {
  private String str;
  public synchronized SafeClass(String s){
    str = s;
    str = str.toUpperCase();
    System.out.println(str+" ");    
  }
}
Options :

a. It will print - ABC XYZ ABC XYZ in that sequence.
b. It will print - ABC ABC XYZ XYZ in that sequence
c. It will print - ABC and XYZ twice, but the sequence cannot be determined.
d. It will print - abc and xyz twice, but the sequence cannot be determined.
e. The code will not compile.


Question 8SocialTwist Tell-a-Friend

Question 7

Which of the following statements are true about synchronization?  (Select two choices)

Options :

A. Static methods cannot be synchronized.

B. Synchronized methods methods cannot make calls to non-synchronized methods

C. A synchronized method can be overridden by a non-synchronized and vice-versa

D. When a Thread is executing a synchronized method of a an object, other threads can freely access non-synchronized methods of the same object.

Question 7SocialTwist Tell-a-Friend

Question 6

Given that a static method doIt() in the Work class represents work to be done, which of the following blocks of code will succeed in starting a new thread that will do the work?

Picture

Question 6SocialTwist Tell-a-Friend

Question 5

There are three Threads trying to invoke a method of an object. This method is a synchronized method to prevent conflict between Threads. While Thread1 is executing the method, Thread2 and Thread3, in that order attempt to invoke the method.
What happens when Thread1 exits the method?

Options : (Choose one answer)

a. Thread2 will be the first waiting Thread to execute the method.

b. Thread3 will be the first waiting Thread to execute the method.

c. The order In which the Ttread2 and Thread3 will invoke the method cannot be determined.

d. None of these.

Question 5SocialTwist Tell-a-Friend

Question 4

What will be the result of an attempt to compile and run the following program? (Choose 1 Answer)

public class ThreadPrediction {
    public ThreadPrediction() {
    }
    public static void main(String[] args){
      ThreadHelper th = new ThreadHelper();
      new Thread(th,"A").start();
      new Thread(th,"B").start();
    }
}
class ThreadHelper implements Runnable{  
  String str = new String("Running");
  public void run(){    
    synchronized(str){
      try{
        System.out.println(Thread.currentThread().getName());
        Thread.sleep(5000);
        System.out.println(Thread.currentThread().getName());
      }catch(InterruptedException ie){
        ie.printStackTrace();
      }
    }
  }
}

Options :

a. Result cannot be predicted
b. Prints "ABAB" or "BABA"
c. Prints "AABB" or "BBAA"
d. Compiler Error
e. An exception is thrown


Question 4SocialTwist Tell-a-Friend

Question 3

What will be the result of an attempt to compile and run the following program? (Choose 1 Answer)

public class ThreadPrediction {
    public ThreadPrediction() {
    }
    public static void main(String[] args){
      ThreadHelper th = new ThreadHelper();
      new Thread(th,"A").start();
      new Thread(th,"B").start();
    }
}
class ThreadHelper implements Runnable{  
  public void run(){
    String str = new String("Running");
    synchronized(str){
      try{
        System.out.println(Thread.currentThread().getName());
        Thread.sleep(5000);
        System.out.println(Thread.currentThread().getName());
      }catch(InterruptedException ie){
        ie.printStackTrace();
      }
    }
  }

}
Options :

a. Result cannot be predicted
b. Prints "ABAB" or "BABA"
c. Prints "AABB" or "BBAA"
d. Compiler Error
e. An exception is thrown


Question 3SocialTwist Tell-a-Friend

Question 2

What will be the result of an attempt to compile and run the following program? (Choose 1 Answer)

public class ThreadPrediction {
    public ThreadPrediction() {
    }
    public static void main(String[] args){
    	new Thread(new ThreadHelper(),"A").start();
    	new Thread(new ThreadHelper(),"B").start();
    }
}
class ThreadHelper implements Runnable{
	public void run(){
	String str = new String("Running");
	synchronized(str){
	try{
	System.out.println(Thread.currentThread().getName());
	Thread.sleep(5000);
	System.out.println(Thread.currentThread().getName());
	}catch(InterruptedException ie){
	ie.printStackTrace();
        }
	}
	}

}
Options :

a. Result cannot be predicted
b. Prints "ABAB" or "BABA"
c. Prints "AABB" or "BBAA"
d. Compiler Error
e. An exception is thrown


Question 2SocialTwist Tell-a-Friend

Question 1

Which of the following lines will not compile ?? Choose Two.

  1: public class Question1 {
  2:     public Question1() {
  3:     }
  4:     public static void main(){
  5:     	Thread t1 = new Thread(); // Line 1
  6:     	Thread t2 = new Thread(new MyClass());//Line 2
  7:     	Thread t3 = new Thread(new MyClass(),"Thread3");//Line 3
  8:     	Thread t4 = new Thread("Thread4");//Line 4
  9:     	Thread t5 = new Thread("Thread5",5);//Line 5
 10:     	Thread t6 = new Thread ("Thread6", new MyClass());//Line 6
 11:     }
 12: }
 13: class MyClass implements Runnable{
 14: 	public void run(){}
 15: }

Options :
a. Line 1
b. Line 2
c. Line 3
d. Line 4
e. Line 5
f. Line 6


Question 1SocialTwist Tell-a-Friend

Sunday, January 23, 2011

Setting PATH and Classpath in Windows.

After installing JDK (download JDK here) we have to set path and classpath variables.

Path variable holds a the path to the executable file javac.exe. Generally this file is located at C:\Program Files\Java\jdk1.6.x\bin. So in order to execute this command we must go to C:\Program Files\Java\jdk1.6.x\bin and execute javac x.java. This is a painful task, so in order to avoid that we must set path variable in the environmental variables.

Setting PATH and Classpath in Windows.SocialTwist Tell-a-Friend