Java OOP - P10
a
What code may be filled in the blank without causing syntax or runtime errors? public class Test { java.util.Date date; public static void main(String[] args) { Test test = new Test(); System.out.println(_________________); } } (a) Test.date (b) Date (c) Test.date.toString() (d) Date.toString() (e) Date.test.
b
What does the expression (int)(76.0252175 * 100) / 100 evaluate to? (a) 76.02 (b) 76 (c) 76.0252175 (d) 76.03 (e) 76.0352715.
e
When you run an applet, which of the following is invoked first? (a) The init method (b) The start method (c) The stop method (d) The destroy method (e) The applet's default constructor.
c
Which of the following assignment statement(s) is/are correct? I. char c = 'd'; II. char c = 100; III. char c = "d"; IV. char c = "100"; (a) Only (I) above (b) Only (II) above (c) Both (I) and (II) above (d) Both (II) and (III) above (e) Both (III) and (IV) above.
c
Analyze the following code and choose the correct answer: public class Foo { private int x; public static void main(String[] args) { Foo foo = new Foo(); System.out.println(foo.x); } } (a) Since x is private, it cannot be accessed from an object foo (b) Since x is defined in the class Foo, it can be accessed by any method inside the class without using an object. You can write the code to access x without creating an object such as foo in this code (c) Since x is an instance variable, it cannot be directly used inside a main method. However, it can be accessed through an object such as foo in this code (d) You cannot create a self-referenced object; that is, foo is created inside the class Foo (e) Since x is public it cannot be accessed from an object foo.
b
Analyze the following code. 1. import java.util.*; 2. public class Test { 3. public static void main(String[] args) { 4. Calendar[] calendars = new Calendar[10]; 5. calendars[0] = new Calendar(); 6. calendars[1] = new GregorianCalendar(); 7. } 8. } (a) The program has a syntax error on Line 4 because java.util.Calendar is an abstract class (b) The program has a syntax error on Line 5 because java.util.Calendar is an abstract class. (c) The program has a syntax error on Line 6 because Calendar[1] is not of a GregorianCalendar type (d) Both (a) and (b) above (e) Both (b) and (c) above.
e
Assume int[] scores = {1, 20, 30, 40, 50}, what value does java.util.Arrays.binarySearch(scores, 3) return? (a) 0 (b) -1 (c) 1 (d) 2 (e) -2.
b
How many bytes a Java character uses for storing? (a) One byte (b) Two bytes (c) Three bytes (d) Four bytes (e) Five bytes.
d
Suppose you define a Java class as follows: public class Test { } In order to compile this program, the source code should be stored in a file named (a) Test. class (b) Test.doc (c) Test.txt (d) Test. java (e) Any name with extension .java.
e
To declare a constant MAX_LENGTH as a member of the class, you write (a) Final static MAX_LENGTH = 99.98; (b) Final static float MAX_LENGTH = 99.98; (c) Static double MAX_LENGTH = 99.98; (d) Final double MAX_LENGTH = 99.98; (e) Final static double MAX_LENGTH = 99.98;.
d
What is the output of the following code? public class Test { public static void main(String[] args) { int[] x = {1, 2, 3, 4, 5}; increase(x); int[] y = {1, 2, 3, 4, 5}; increase(y[0]); System.out.println(x[0] + " " + y[0]); } public static void increase(int[] x) { for (int i = 0; i < x.length; i++) x[i]++; } public static void increase(int y) { y++; } } (a) 0 0 (b) 1 1 (c) 2 2 (d) 2 1 (e) 1 2.
c
What is the printout for the second statement in the main method? public class Foo { static int i = 0; static int j = 0; public static void main(String[] args) { int i = 2; int k = 3; { int j = 3; System.out.println("i + j is " + i + j); } k = i + j; System.out.println("k is " + k); System.out.println("j is " + j); } } (a) k is 0 (b) k is 1 (c) k is 2 (d) k is 3 (e) k is -1.
d
What is the printout of the following code? double x = 5.5; int y = (int)x; System.out.println("x is " + x + " and y is " + y); (a) x is 5.0 and y is 6.0 (b) x is 6.0 and y is 6.0 (c) x is 6 and y is 6 (d) x is 5.5 and y is 5.0 (e) x is 5.5 and y is 6.5.
b
What is the printout of the second println statement in the main method? public class Foo { int i; static int s; public static void main(String[] args) { Foo f1 = new Foo(); System.out.println("f1.i is " + f1.i + " f1.s is " + f1.s); Foo f2 = new Foo(); System.out.println("f2.i is " + f2.i + " f2.s is " + f2.s); Foo f3 = new Foo(); System.out.println("f3.i is " + f3.i + " f3.s is " + f3.s); } public Foo() { i++; s++; } } (a) f2.i is 1 f2.s is 1 (b) f2.i is 1 f2.s is 2 (c) f2.i is 2 f2.s is 2 (d) f2.i is 2 f2.s is 1 (e) f2.i is 0 f2.s is 1.
c
What is the printout of the third println statement in the main method? public class Foo { int i; static int s; public static void main(String[] args) { Foo f1 = new Foo(); System.out.println("f1.i is " + f1.i + " f1.s is " + f1.s); Foo f2 = new Foo(); System.out.println("f2.i is " + f2.i + " f2.s is " + f2.s); Foo f3 = new Foo(); System.out.println("f3.i is " + f3.i + " f3.s is " + f3.s); } public Foo() { i++; s++; } } (a) f3.i is 1 f3.s is 1 (b) f3.i is 1 f3.s is 2 (c) f3.i is 1 f3.s is 3 (d) f3.i is 3 f3.s is 1 (e) f3.i is 3 f3.s is 3.
b
What is wrong in the following code? class TempClass { int i; public void TempClass(int j) { int i = j; } } public class C { public static void main(String[] args) { TempClass temp = new TempClass(2); } } (a) The program has a compilation error because TempClass does not have a default constructor (b) The program has a compilation error because TempClass does not have a constructor with an int argument (c) The program compiles fine, but it does not run because class C is not public (d) The program compiles and runs fine (e) All of the above.
e
What would be the result while attempting to compile and run the following code? public class Test { public static void main(String[] args) { double[] x = new double[]{1, 2, 3}; System.out.println("Value is " + x[1]); } } (a) The program has a syntax error because the syntax new double[]{1, 2, 3} is wrong and it should be replaced by {1, 2, 3}. (b) The program has a syntax error because the syntax new double[]{1, 2, 3} is wrong and it should be replaced by new double[3]{1, 2, 3}; (c) The program has a syntax error because the syntax new double[]{1, 2, 3} is wrong and it should be replaced by new double[]{1.0, 2.0, 3.0}; (d) The program compiles and runs fine and the output "Value is 1.0" is printed (e) The program compiles and runs fine and the output "Value is 2.0" is printed.
d
Which of the following is the correct expression of character 4? (a) 4 (b) "4" (c) '\0004' (d) '4' (e) '\4'.
b
Which of the following statement is not true? (a) A default no-arg constructor is provided automatically if no constructors are explicitly declared in the class (b) At least one constructor must always be defined explicitly (c) Constructors do not have a return type, not even void (d) Constructors must have the same name as the class itself (e) Constructors are invoked using the new operator when an object is created.
a
Which of the follows JDK command is correct to run a Java application in ByteCode.class? (a) java ByteCode (b) java ByteCode.class (c) javac ByteCode.java (d) javac ByteCode (e) JAVAC ByteCode.