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++;
}
}
|
||||||||||
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++;
}
}
|
|||||||||||
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(_________________);
}
}
|
|||||||||||
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);
}
}
|
|||||||||||
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);
}
}
|
|||||||||||
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]);
}
}
|
|||||||||||
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++;
}
}
|
|||||||||||
Assume
int[] scores = {1, 20, 30, 40, 50}, what value does
java.util.Arrays.binarySearch(scores, 3) return?
|
|||||||||||
When
you run an applet, which of the following is invoked first?
|
|||||||||||
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.
}
|
Answers
191.
|
Answer
: (b)
Reason: i is an instance variable and s is static,
shared by all objects of the Foo class.
|
Answer
: (c)
Reason: i is an instance variable and s is static,
shared by all objects of the Foo class.
|
|
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.
|
|
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).
|
|
Answer
: (c)
Reason: When computing k = i + j; i = 2 and
j = 0.
|
|
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};
|
|
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.
|
|
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.
|
|
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.
|
|
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 >>
thank you.
ReplyDeletejava tutorial
java tutorial
ReplyDeleteVery wonderful informative article. I appreciated looking at your article. Very wonderful reveal. I would like to twit this on my followers. Many thanks!
Acostarse Conjugation in Spanish
Dimensional Analysis Calculator
Electric Force
Gender Schema Theory
Combination Reaction