Java Programming Questions and Answers Set 20

Java Programming OOPs

Questions 191 to 200



191.
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.
192.
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.
193.
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.
194.
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.
195.
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.
196.
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.
197.
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.
198.
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.
199.
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.
200.
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.




Answers



191.
Answer : (b)
Reason:  i is an instance variable and s is static, shared by all objects of the Foo class.
192.
Answer : (c)
Reason:  i is an instance variable and s is static, shared by all objects of the Foo class.
193.
Answer : (a)
Reason:  b and d cause syntax errors because date is an instance variable and cannot be accessed from static context. c is wrong because test.date is null, causing NullPointerException.
194.
Answer : (c)
Reason:  (A) is incorrect, since x can be accessed by an object of Foo inside the Foo class. (B) is incorrect because x is non-static, it cannot be accessed in the main method without creating an object. (D) is incorrect, since it is permissible to create an instance of the class within the class.(E) is incorrect as x is declared as private but not public.The best choice is (C).
195.
Answer : (c)
Reason:  When computing k = i + j;  i = 2 and  j = 0.
196.
Answer : (e)
Reason:  new double[]{1, 2, 3} is correct. This is the syntax. In this question, double[] x = new double[]{1, 2, 3} is equivalent to double[] x = {1, 2, 3};
197.
Answer : (d)
Reason:  Invoking increase(x) passes the reference of the array to the method. Invoking increase(y[0]) passes the value 1 to the method. The value y[0] outside the method is not changed.
198.
Answer : (e)
Reason:  The binarySearch method returns the index of the search key if it is contained in the list. Otherwise, it returns ?insertion point - 1. The insertion point is the point at which the key would be inserted into the list. In this case the insertion point is 1. Note that the array index starts from 0.
199.
Answer : (e)
Reason:  When the applet is loaded to the Web browser, the Web browser creates an instance of the applet by invoking the applets default constructor.
200.
Answer : (b)
Reason:  (A) is incorrect since it is OK to use abstract class as data type for arrays. new Calendar[10] does not create Calendar objects. It just creates an array with 10 elements, each of which can reference to a Calendar object. (B) is correct since you cannot create an object from an abstract class. (C) is incorrect since it is fine to create a GregorianCalendar object and assign its reference to a variable of its superclass type.



<< Prev   1   2   3   4   5   6   7   8   9   10   11   12   13   14   15   16   17   18   19   20   21   22  

 23   24   25   26   27   28   29   30  Next >>


2 comments :