Thursday, December 20, 2012

Java tests

Question 1: You are planning to do an indexed search in a list of objects. Which of the two Java collections should you use:
  1. ArrayList
  2. LinkedList
   
Question 2: Inner classes can be defined within methods.
  1. False
  2. True
Question 3: The default statement of a switch is always executed.
  1. False
  2. True
Question 4: What is the size of a Char?
  1. 16 bits
  2. 4 bits
  3. 7 bits
  4. 8 bits
Question 5: The following code will result in:

class A {
  public static void main(String [] args) {
    B b = new A();
  }
}
class B extends A {
}

  1. Compile error
  2. No error
  3. Runtime Exception
Question 6: Can you compare a boolean to an integer?   

Question 7: Can you call one constructor from another if a class has multiple constructors?
Answer: yes:
public class Foo
{
    private int x;

    public Foo()
    {
        this(1);
    }

    public Foo(int x)
    {
        this.x = x;
    }
}

Question 8: For how long will the thread wait after calling wait(1000)?
  1. 1 second
  2. 1000 seconds
  3. until it receives notify() or notifyAll() calls
  4. up to 1 second
Question 9: With the following code what happens when you do if (a==b)? False!
Integer a = new Integer(2);
Integer b = new Integer(2);
  1. Compiler error
  2. False
  3. Runtime Exception
  4. True
Question 10: A class cannot be declared: ?????
Default
Private
Static

Question 11: Can you write a Java class that could be used both as an applet as well as an application?

Question 12: The following code will result in:
class A {
  int b = 1;
  public static void main(String [] args) {
    System.out.println("b is " + b);
  }
}
  1. Compilation error
  2. Output of b is 1
  3. Runtime Error
  4. Runtime Exception
Question 13: If the code in the try block attempts to open an non-existing file
Will you ever see File not found printed out?

try {
  // ... some code ...
} catch (IOException e) {
  System.out.println("IO Error");
} catch (FileNotFoundException e) {
  System.out.println("File not found");
}
  1. No
  2. Will not compile
  3. Yes 
Question 14: java assert

Question 15: Following code will result in:
int a = 9/0;
  1. Compilation error: DivideByZeroException
  2. Compilation error: Divisions must be in a try block.
  3. No Error: a is NaN
  4. Runtime Exception
Question 16: What will happen?
public class Static
{
      static
      {
            int x = 5;
      }
      static int x,y;
      public static void main(String args[])
      {
            x--; myMethod();
            System.out.println(x + y + ++x);
      }
      public static void myMethod()
      {
             y = x++ + ++x;
      }
}

Question 17: Can an inner class declared inside of a method access local variables of this method?
No
Only final variables
Only variables of primitive types
Yes, always

Question 18: A class can be transient
  1. Primitive datatypes are allocated on a stack?
  2. Java only allocates primitive data types like int and double and object references on the stack. All objects are allocated on the heap.
Question 19: Which method is not member of Object?
  1. clone
  2. compare
  3. equals
  4. finalize
  5. notify
Question 20: Which directory should you add to the CLASSPATH environment variable?
You want to access the class MyClass from package com.my.somestuff
The file is here:
C:\javaworks\project1\com\my\somestuff\MyClass.java
  1. C:\javaworks
  2. C:\javaworks\project1
  3. C:\javaworks\project1\com\my
  4. C:\javaworks\project1\com\my\somestuff

No comments:

Post a Comment