oDesk Java programming test

Java Programming Test Answer
1. Question: What is the java.net.IDN class in 1.6?
Answer: Methods to convert internationalized domain names (IDNs) between a normal Unicode representation and an ASCII Compatible Encoding (ACE) representation
2. Question: What will be the output of the following program?public class Test
{
public static void main (String args[]) throws Exception
{
Test o = new Test ();
System.out.println (o.content ());
}
public String content () throws Exception
{
throw new Exception (“This is an exception on this.content ()”);
}
private static class B
{
public String content ()
{
return ”B”;
}
}
private static class A extends B
{
public String content ()
{
return ”A”;
}
}
}
Answer: The code will compile but throw an exception at runtime
3. Question: Which of these interfaces are used by implementations of models for JTable?
Answer: TableColumnModel
4. Question: How many times can classes be nested within a class?
Answer: Any number of times
5. Question: One method in your application needs to be synchronized. Which of the following options are correct for synchronization?
Answer: public void Process(){ synchronized(this){ } }
public void synchronized Process(){}
6. Question: What could be the replacement of “//ABC” in the following code?public class Jam
{
public void apple(int i, String s)
{}
//ABC
}
Answer: public void apple(String s, int i){}
public void Apple(int i, String s) {}
7. Question: 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);
}
}
The code will print 2.5
8. Question: 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”);
}
}
Answer: The code will compile. A runtime error will occur because ‘main’ is not properly defined
9. Question: What will be the output when this code is compiled and run?public class Test
{
static int x = 10;
public Test ()
{
Bar b = new Bar ();
Bar b1 = new Bar ();
update (b);
update (b1);
}
private void update (Bar bar)
{
bar.x = ++x;
System.out.println (bar.x);
}
public static void main (String args[])
{
File f = new File(“/”,”autoexec.bat”); b.DataInputStream d = new DataInputStream(System.in);c.RandomAccessFile r = new RandomAccessFile(“OutFile”); d.OutputStreamWriter o = newOutputStreamWriter(System.out);
}
private class Bar
{
public int x = 10;
}
}
The code will fail to compile
Answer: 11 12
10. Question: Which of the following statement will not compile?
Answer: RandomAccessFile r = new RandomAccessFile(“OutFile”);
11. Question: Which of the following are “keywords” in Java?
Answer: default
12. Question: Which of the following statements is true of the HashMap class?
Answer: It stores information as key/value pairs
13. Question: How does the set collection deal with duplicate elements?
Answer: The add method returns false if you attempt to add an element with a duplicate value
14. Question: 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();
}
}
Answer: None of the above
15. Question: Is the following code valid?InetAddress ad = InetAddress.getByName (“195.186.2.111″);
Answer: Yes
16. Question: Which of the following cannot apply to constructors?
Answer: Void return type
17. Question: Choose the correct declarations for the main() method which will allow the class to be run as a standalone program.
Answer: public static void main(String str[])
18. Question: For a class defined inside a method, what rule governs access to the variables of the enclosing method?
Answer: The class can only access transient variables
19. Question: A method can be defined as native to:
Answer: Get to access hardware that Java does not know about
Write optimized code for performance in a language such as C/C++
20. Question: What will be the output of the following line?System.out.println(Math.floor(-2.1));
Answer: -3.0
21. Question: 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”;
Answer: s+=i;

No comments: