What gets created? (10)

 

Please circle all of the correct answers for each problem.  There may be problems for which all of the answers are correct.  If all the answers are correct for a specific problem, you should circle all of the choices.  There may be problems for which there are no correct answers.  If there are no correct answers for a specific problem, you should not circle any of the choices. 

 

1. The Java statement   public class LinkedListStack {}   creates:  ANSWER: A

   (a)  new class; (b) new object; (c) new reference variable; (d) new container to hold objects

 

2.   The Java statement   Vector elements = new Vector();   creates: ANSWER: B ,C, D

   (a)  new class; (b) new object; (c) new reference variable; (d) new container to hold objects

 

3.              The Java statement   Vector[] elements = null;   creates: ANSWER: C

   (a)  new class; (b) new object; (c) new reference variable; (d) new container to hold objects

 

4.   The Java statement   Object element = new Object();   creates: ANSWER: B, C

   (a)  new class; (b) new object; (c) new reference variable; (d) new container to hold objects

 

5.   The Java statement   Object element = new Vector();   creates: ANSWER: B ,C, D

   (a)  new class; (b) new object; (c) new reference variable; (d) new container to hold objects

 

Java true/false questions.  (25)

                                          

[T] or [F] : The constructor of a class must not have a return type. [T]

[T] or [F] : A class that is abstract may not be instantiated. [T]

[T] or [F] : The final keyword indicates that the body of a method is to be found elsewhere. The code is written in non-Java language, typically in C/C++.  [F]

[T] or [F] : A static variable indicates there is only one copy of that variable. [T]

[T] or [F] : A method defined as private indicates that it is accessible to all other classes in the same package. [F]

[T] or [F] : For each try block there must be at least one catch block defined. [F]

[T] or [F] : A try block may be followed by any number of finally blocks. [F]

[T] or [F] : A try block must be followed by at least one finally or catch block. [T]

[T] or [F] : If both catch and finally blocks are defined, catch block must precede the finally block. [T]

[T] or [F] : Arrays in Java are essentially objects. [T]

[T] or [F] : It’s not possible to assign one array to another. Individual elements of array can however be assigned. [F]

[T] or [F] : Array elements are indexed from 1 to size of array. [F]

[T] or [F] : If a method tries to access an array element beyond its range, a compile warning is generated. [F]

[T] or [F] : Each Java file must have exactly one package statement to specify where the class is stored. [F]

[T] or [F] : If a Java file has both import and package statements, the import must precede the package. [F]

[T] or [F] : A Java file must have at least one class definition. [F]

[T] or [F] : If a Java file has a package statement, it must be the first statement except for possible  comments. [T]

[T] or [F] : The Java Virtual Machine compiles Java byte code into machine language instructions. [F]

[T] or [F] : Java does not have true multiple inheritance like C++. [T]

[T] or [F] : Java applets, but not applications, are run in a “sandbox” that limits the allowed operations. [T]

[T] or [F] : A Java jar file contains Java source code, a manifest, and documentation. [F]

[T] or [F] :  As the toString method is defined in the Object class,

 System.out.println can be used to print any object. [T]

[T] or [F] :

[T] or [F] :

[T] or [F] :

 

Java multiple choice (2 points each)

 

What keyword is used to make a variable belong to a class, rather than  being defined for each instance of the class? (a) static (b) final (c) abstract (d) native (e) volatile (f) transient. ANSWER: a

 

The default layout manager for a Frame is ... (a) FlowLayout (b) BorderLayout (c)GridLayout (d) GridBagLayout (e) CardLayout  ANSWER: (b)

 

Assume that class A extends class B, which extends class C. Also all the three classes implement the method test(). How can a method in a class A invoke the test() method defined in class C (without creating a new instance of class C). Select the one correct answer. (a) test(); (b)  super.test(); (c) super.super.test(); (d) ::test(); (e) C.test(); (f) It is not possible to invoke test() method defined in C from a method in A. ANSWER: F

 

 

Consider that Parent and Child classes are defined in two different files as below:

 

class Parent{

  public Parent(){

  System.out.println("Parent");

  }

}

 

class Child extends Parent{

  public Child(int x){

  System.out.println("Child");

}

 

public static void main(String [] args){

  Child c=new Child(10);

  }

}

 

What will be output if you try to compile and run above program? (a) It will not compile. (b) It will compile successfully and print "Parent" and then "Child." (c) It will compile successfully and print "Child" and then "Parent." (d) It will compile successfully, but not run.  ANSWER: B

 

 

Consider following code:

 

  public class OuterClass{

  class InnerClass{  }

  public void innerClassDemo(){

    //Explicit instance of InnerClass

    }}

How can you explicitly create an instance of InnerClass? (a) InnerClass i=InnerClass(); (b) InnerClass

i=OuterClass.InnerClass(); (c) InnerClass i=new OuterClass ().new

InnerClass(); (d) OuterClass.InnerClass i=new OuterClass.InnerClass(); ANSWER: C and D

 

 

Consider the following code:

 

  /** File Thread1.java */

  class Thread1 implements Runnable{

    public void run(){

      System.out.println("Running Thread1");

      }

  }  /** End of file Thread1.java */

 

  /** Thread2.java */

  class Thread2 extends Thread{

    public void run(){

      System.out.println("Running Thread2");

      }

 

    public static void main(String [] args){

      Thread1 t1= new Thread1();

      Thread t2=new Thread2(t1);

      t1.start();

      t2.start();

      }

  }  /** End of Thread2.java*/

If you try to compile and run above code what will be result?  (a) "Running thread1" following "Running thread2", (b) "Running thread2" following "Running thread1", (c) It will not compile because in Thread1 and Thread2 start()  is not defined ., (d) It will not compile because  constructor invoked to create Thread2 with arguments (Thread1) is not defined. ANSWER: D

 

 

Consider that class Employee and Salesman are in different file called Employee.java and Salesman.java:

 

  /** Employee.java file*/

  public class Employee{

  int salary=1000;

  public int getSalary(){return salary;}

  } /**  End of Employee.java file*/

 

  /** Salesman.java file*/

  public class Salesman extends Employee{

  int commission =100;

  public int getSalary(){return salary+commission;}

  public static void main(String [] args){

    Salesman sm = new Salesman();

    Employee em = sm;

    System.out.println(em.getSalary());

    }

  } /**  End of Salesman.java file*/

 

 

What will be result if you try to compile and run above code?  (a) Compiler error reported , "Type mismatch: Cannot convert from Salesman to Employee.", (b) It compile successfully and outputs 1000., (c) It compiles successfully and outputs 1100., (d) None of the above ANSWER: C

 

How can you declare a overloaded method? (a) Reusing the method name with different arguments and same return type. (b) Reusing the method name with different arguments and different return type. (c)  Reusing the name with identical arguments and return type. (d) None of the above.  ANSWER: A, B

When can't you override a method? (a) When method is declared abstract. (b)  When method is declared final. (c) When method is declared private. (d) When method is declared static.  ANSWER: B

 

 

Java short answers (4 points each)

 

What is a Java expression that can be used to represent number of elements in an array named arrayOne ? ANSWER: arrayOne.length

 

To represent characters, Java does not use the venerable ASCII but instead uses UNICODE.  Briefly describe the difference between ASCII and UNICODE and give two motivations for moving to UNICODE.  ASCII encodes a character in eight bits whereas UNICODE uses 16 bits.  Thus UNICODE can represent many more characters.  UNICODE is a standard that includes (1) more special characters for logic, math, etc. and (2) characters required by other languages.