Review 4 (Year 3 Sem 1)

Review 4 is the fourth review in semester 1 of year 3, and the fourth review in the total of year 3. It is a review for classes, and is considered to be moderately easy.

Description
Please note that the purpose of Review Exercises is to prepare you for the WRITTEN TEST.

Hence, please do the questions after revising the topics, and think through the options properly.

You should NOT try to execute the code when attempting the questions.

Unlocked after completion

 * OCD (Score at least 60% for Review Exercise 4 [C7 & C8])
 * All reviewed (Score at least 80% for Review Exercise 4 [C7 & C8])
 * Hurdle Class (Score at least 80% for Review Exercise 4 [C7 & C8])

Question
Consider the following class definitions: public class Test {   int i ; } public class Main {   public static void main(String args[]) {        Test t;        System. out .println(t.i);   } } Predict the output when class Main is executed.

Options

 * 0
 * Compile time error
 * Garbage value will be printed
 * Runtime error
 * None of the above.

Question
Consider the following class definitions: public class Test {   int i ; } public class Main {   public static void main(String args[]) {        Test t = new Test;        System. out .println(t.i);   } } Predict the output when  class Main is executed.

Options

 * 0
 * Compile time error
 * Garbage value will be printed
 * Runtime error
 * None of the above.

Question
Which of the following are true? (Choose all that apply) public class Bunny {   public static void main(String[] args) {        Bunny bun = new Bunny;    } }

Options
◻ main is a reference to an object

◻ Bunny is class

◻ main is a class

◻ bun is a class

◻ bun is a reference to an object

◻ Bunny is a reference to an object

Question
Which are true of the following code? (Choose all that apply) public class Plane {   String name ;     void fly{        System. out .println( name + " is flying." );   }     void takeOff{        System. out .println( name + " is taking off." );   } } public class TestPlane {    public static void main(String[] args) {        Plane p = new Plane;        Plane q = new Plane;         try {            p. name = "F16" ;            p.takeOff;            p = q;            p.fly;            q. name = "F5" ;            p.takeOff;            q.takeOff;            q = null ;            q.fly;        }        catch (IllegalArgumentException ex){            System. out .println( "Illegal Argument." );       }        catch (ArithmeticException ex){            System. out .println( "Arithmetic Problem." );       }        catch (Exception e){            System. out .println( "Engine Failure." );       }    }  } 

Options
◻ “null is flying.” will be printed

◻ “Illegal Argument.” will be printed

◻ q is garbage collected

◻ “Engine Failure.” will be printed

◻ The code compiles.

◻ F16 is flying will be printed.

Question
Suppose we have a class named Rabbit. Which of the following statements are true? (Choose all that apply)

Options
◻ The Rabbit object from line 3 is first eligible for garbage collection immediately following line 6.

◻ The Rabbit object from line 3 is first eligible for garbage collection immediately following line 8.

◻ The Rabbit object from line 3 is first eligible for garbage collection immediately following line 12.

◻ The Rabbit object from line 4 is first eligible for garbage collection immediately following line 9.

◻ The Rabbit object from line 4 is first eligible for garbage collection immediately following line 11.

◻ The Rabbit object from line 4 is first eligible for garbage collection immediately following line 12.

Question
Consider the following class definitions: public class Test {   int i ;     void doSomething{        System. out .println( "Did something" );   } } public class Main {   public static void main(String args[]) {        Test t = null ;        t.doSomething;    } } Predict the output when  class Main is executed.

Options

 * 0
 * Compile time error
 * Garbage value will be printed
 * Runtime error
 * None of the above

Solution
NullpointerException

Question
Consider the following class definitions: public class Demo {   int a = 10 ;    int b = 20 ;     void print {        System. out .println ( "a = " + a + " b = " + b );   } } public class Test {    public static void main(String[] args) {        Demo obj1 = new Demo;        Demo obj2 = obj1;         obj1. a += 1 ;       obj1. b += 1 ;        System. out .print( "values of obj1: " );       obj1.print;        System. out .print( "values of obj2: " );       obj2.print;     } } Predict the output when class Test is executed.

Options

 * values of obj1: a = 11 b = 21 values of obj2: a = 10 b = 20
 * values of obj1: a = 11 b = 20 values of obj2: a = 11 b = 21
 * values of obj1: a = 11 b = 21 values of obj2: a = 10 b = 21
 * values of obj1: a = 11 b = 21 values of obj2: a = 11 b = 21
 * Compile time error

Question
What is the result of the following code when executed? class Employee {   private int employeeId ;    private String firstName, lastName ;    public Employee {        this ( "Mickey" , "Mouse" );    }    public Employee(String f, String l){        this. firstName = f;       this. lastName = l;   }    public boolean equals(Employee e) {        return this. employeeId == e. employeeId ;   }    public void setId( int id){        this. employeeId = id;   }    public String getFirstName{        return firstName ;    } }  public class testEmployee {    public static void main(String[] args){        Employee one = new Employee( "Donald", "Duck" );        one.setId( 101 );        Employee two = new Employee;        two.setId( 101 );        if (one.equals(two))            System. out .print( "Success " );       else             System. out .print( "Failure " );        if (one.getFirstName.equals(two.getFirstName))            System. out .println(one.getFirstName);       else             System. out .println(two.getFirstName);   } }

Options

 * Failure Mickey
 * Success Mickey
 * Failure Donald
 * Success Donald
 * Success

Question
What is the output of the following program? 1. class Color { 2. private int red, green , blue ; 3. Color { 4. Color( 10, 10 , 10 ); 5. } 6.   Color( int r, int g, int b) { 7. red = r; 8. green = g; 9. blue = b; 10. } 11.  void printColor { 12. System. out .println( "R: " + this . red + " G: " + this . green + " B: " + this . blue ); 13. } 14.   public static void main(String [] args){ 15. Color color = new Color; 16. color.printColor; 17. } 18.}

Options

 * Compiles without errors. It prints the following: R: 0 G: 0  B: 0.
 * Compiles without errors. It prints the following: R: 10 G: 10  B: 10.
 * Compiles without errors, and when run, crashes by throwing NullPointerException.
 * Compiler error in line 4
 * Compiler error in line 12

Question
Which statement is true about the accessibility of data members?

Options

 * A private data member is always accessible within the same package.
 * A private data member can be accessed only within the class of the data member.
 * A data member with package accessibility can be accessed by any class from another package.
 * A private data member cannot be accessed at all.
 * Package accessibility for a data member can be declared using the keyword package.

Question
Which statement is true?

Options

 * A static method can call other non-static methods in the same class by using the this keyword.
 * Instance methods may access local variables of static methods.
 * Each object of a class has its own instance of the static variables declared in the class.
 * A class may contain both static and non-static variables, and both static and non-static methods.
 * All methods in a class are implicitly passed the this reference as an argument when invoked.

Question
Write the output when class Q is executed. class T {   private static int value1 = 123 ;    private int value2 ;    public void increment {        value2 = value1 ++;    }    public int getValue1 {        return value1 ;    }    public int getValue2 {        return value2 ;    } }  public class Q {    public static void main(String[] args) {        T t1 = new T;        T t2 = new T;        t1.increment;        t2.increment;        System. out .print(t1.getValue1);       System. out .print(t1.getValue2);       System. out .print(t2.getValue1);       System. out .print(t2.getValue2);   } }

Question
What is the result of the following code when executed? class Course {   public String courseName ;    public int courseID ; }  public class Tester{     public static void main(String[]args) {        Course course1 = new Course;        course1. courseID = 0001 ;       course1. courseName = "0001" ;        Course course2 = new Course;        course2 = course1;        course1. courseID ++;              System.out  .println(course1. courseID  + course2. courseID  );     } }

Options

 * 1
 * 11
 * 2
 * 22
 * 4
 * Compile time error

Question
What is the result of the following code when executed? public class Test{   private int x ;    public static void main(String[]args) {        System. out .println( x );   } }

Options

 * 0
 * Run-time error
 * Compile time error as non-static x cannot be reference from a static context.
 * Compile time error as x is not initialized.
 * Compile time error as x is of private access.

Question
What is the result of the following code when executed? class Test {   public static void main(String[] args) {        Test obj = new Test;        obj.start;    }     void start {        String stra = "do" ;        String strb = method(stra);        System. out .print( ": " +stra + strb);   }     String method(String stra) {        stra = stra + "good" ;        System. out .print(stra);       return "good" ;    } }

Options

 * dogood : dogoodgood
 * dogood : gooddogood
 * dogood : dodogood
 * dogood : dogood
 * Compile time error

Question
What is the result of the following code when executed? class Test {   int a ;    int b ;    Test {        a = 10 ;        b = 20 ;    }     Test get{        a ++;        return this ;    }     void display {        System. out .println( "a = " + a + " b = " + b );    }     public static void main(String[] args){        Test object = new Test;        object.get.display;    } }

Options

 * a = 0 b = 0
 * a = 10 b = 20
 * a = 11 b = 20
 * Run-time error
 * Compile time error

Question
What is the result of the following code when executed? public class B {   int b ;    public B( int b) {        b = b;    }    public static void main (String[] args){        B x = new B( 10 );        System. out .println(x. b );   } }

Options

 * 0
 * 10
 * null
 * Run-time error
 * Compile time error

Question
What is the result of the following code when executed? public class C{   private String msg = "Bye" ;    public C{        this ( "Hello" );    }    public C(String str){        msg = str + " World" ;    }    public String toString{        return msg ;    }    public static void main (String[] args){        C obj = new C;        System. out .println(obj);   } }

Options

 * Bye
 * Hello
 * Hello World
 * Memory address of obj
 * Compile time error