Sunday, June 30, 2013

Java Programming Test Question and Answers






Java Programming Test Question and Answers


Q1. Which of the following are “keywords” in Java?

a. default

b. NULL

c. String

d. throws


Q2. Which of these interfaces are used by implementations of models for JTable?

a. TableModel

b. TableColumnModel

c. TableSelectionModel

d. ListModel


Q3. What could be the replacement of “//ABC” in the following code?


public class Jam

{

public void apple(int i, String s)

{}

//ABC

}


a. public void apple(String s, int i){}

b. public int apple(int i, String s){}

c. public void apple(int i, String mystring){}

d. public void Apple(int i, String s) {}


Q4. What is the java.net.IDN class in 1.6?

a.Methods to resolve integrated domain names (IDNs), such domain names are special embedded names

b. Methods to swap bytes between network byte order and host byte order

c. Methods to convert internationalized domain names (IDNs) between a normal Unicode representation and an ASCII

d. Compatible Encoding (ACE) representation

e. This class does not exist


Q5. How many times can classes be nested within a class?

a. 5

b. 8

c. 4

d. Any number of times


Q6. What will be the output when the following code is compiled and run?


public class Test

{

public static void main (String args[])

{

int i;

i = 3;

System.out.println ((int)i * 2.5 / 3.0);

}

}


a. The code will compile, but it will throw an exception when it is run

b. The code will compile and it will not produce any output when it is run

c. The code will fail to compile

d. The code will print 3

e. The code will print 2.5

f. The code will print 2


Q7.Which of the following statement will not compile?

a. File f = new File(“/”,”autoexec.bat”);

b. DataInputStream d = new DataInputStream(System.in);

c. RandomAccessFile r = new RandomAccessFile(“OutFile”);

d. OutputStreamWriter o = new OutputStreamWriter(System.out);


Q8.What would happen on trying to compile and run the following code?


public class MainCls

{

public static void main(String argv)

{

System.out.println(“My Text”);

}

}


a. A compile error will be generated because ‘main’ is a reserved word and cannot be used for a class

b. “My Text” will be displayed

c. The code will compile. A runtime error will occur because ‘main’ is not properly defined

d. The code will compile. A runtime error will occur because constructor is not defined


Q9. Which of the following statements is true of the HashMap class?

a. It stores information as key/value pairs

b. Elements are returned in the order they were added

c. It does not permit null keys

d. It does not permit null values


Q10. One method in your application needs to be synchronized. Which of the following options are correct for synchronization?

a. public synchronized void Process(void){}

b. public void Process(){ synchronized(this){ } }

c. public void synchronized Process(){}

d. public synchronized void Process(){}


Q11.What is wrong with the following code?


class X extends Exception {}

public class Y

{public void foo()

{

try {

b();

}

finally {

ba();

}

catch (MyException e) {}

}

public void b() throws X {

throw new X();

}public void ba() throws RuntimeException {

throw new RuntimeException();

}

}


a. Nothing is wrong with the code

b. Finally block should come after the catch block

c. An empty catch block is not allowed

d. None of the above


Q12. Is the following code valid?


InetAddress ad = InetAddress.getByName (“195.186.2.111″);

a. Yes

b. No


Q13.Which of the following cannot apply to constructors?

a. Name same as class name

b. Void return type

c. Can have parameters

d. Overloading


Q14. How does the set collection deal with duplicate elements?

a. Duplicate values will cause an error at compile time

b. A set may contain elements that return duplicate values from a call to the equals method

c. An exception is thrown if you attempt to add an element with a duplicate value

d. The add method returns false if you attempt to add an element with a duplicate value


Q15. For a class defined inside a method, what rule governs access to the variables of the enclosing method?

a. The class can only access transient variables

b. The class can only access static variables

c. The class can only access final variables

d. The class can access any variable


Q16. For the given variables, which of the following will compile without an error?


char c = ‘c’;

int i = 50;

double d = 80;

long l = 200;

String s = “Goodbye”;


a. s+=i;

b. i+=s;

c. c+=s;

d. c=c+i;


Q17. A method can be defined as native to:

a. Overcome the limitation of the private scope of a method

b. Get to access hardware that Java does not know about

c. Write optimized code for performance in a language such as C/C++

d. Define a new data type such as an unsigned integer


Q18. What will be the output of the following line?System.out.println(Math.floor(-2.1));

a. -2

b. 2.0

c. -3

d. -3.0


Q19.Choose the correct declarations for the main() method which will allow the class to be run as a standalone program.

a. public void main(String str[])

b. static public void main(String str[])

c. public static int main(String str[])

d. public static void main(String str[])

No comments:

Post a Comment