No
Question # : 22. Is this an example of an infinite loop?
int x = 10;
do {
x = x + 1;
System.out.println(x);
x = x % x;
} while (x == 0);
Don't rush, think carefully.
- Yes
- No
Question # : 23. What is wrong with this class definition?
class Avg {
public static void main(String[] args) {
double a = 1;
double b = a + 1;
double c = b + 1;
System.out.println(findAvg(a, b, c));
}
double findAvg(double a, double b, double c) {
return (a + b + c) / 3.0;
}
}
How can you fix it so that it works when you invoke main()
?
- turn
findavg
into a public
method
- turn
findavg
into a static
method
- turn
findavg
into a void
method
- none of the above
Question # : 24. Consider the following code:
class One {
static int var1 = 1;
static int var2;
public static void main(String[] args) {
int var3 = var1 + var2;
System.out.println(var1 + var2 + var3);
}
}
What is the result of trying to compile and run it?
-
0
-
1
- The code does not compile because
var3
is not initialized correctly.
- The code does not compile because
var2
is not initialized at all.
- none of the above
Question # : 25. Can you initialize an array of three boolean
values to all true
the way presented below?
boolean[] b;
b = { true, true, true };
- Yes.
- No.
Question # : 26. By default integer values are of type int
,
and floating point values are of type double
. However, you can force
an integer value to be a long
by placing an L
after it.
You can also force a floating-point value to be a float
by placing
an F
after it.
Knowing this, consider the following program:
class Example {
public static void main(String[] args) {
Example e = new Example();
e.test(5);
}
int test(int i) {
System.out.println("long");
return 1;
}
void test(long i) {
System.out.println("int");
}
}
What is the result of attempting to compile and run it?
- The program does not compile because the compiler
cannot distinguish between the two
test()
methods provided.
- The program compiles and runs and "
long
"
appears in the standard output.
- The program compiles and runs and "
int
"
appears in the standard output.
- The program compiles and runs and "
long
"
appears in the standard output followed by a 1
.
Question # : 27. Which of these defines a valid main()
method
(that will run when you invoke java
on the class that contains the method,
from the command line)?
-
public static void main(String args) { }
-
public static void main(String[]) { }
-
public static void main(string[] args) { }
-
public static void main(args) { }
- none of the above
Question # : 28. How can you access the word "jini"
from the
following command-line invocation:
java java jini java jini java
-
args[1]
-
args[2]
-
args[3]
-
args[4]
-
args[5]
Please mark just one answer on the answer sheet.
Question # : 29. What will appear in the
standard output when you run the Tester
class
below?
class Tester {
int var;
Tester (double var) { this.var = (int)var; }
Tester (int var) { this("hello"); }
Tester (String s) { this(); System.out.println(s); }
Tester () { System.out.println("good-bye"); }
public static void main(String[] args) {
Tester t = new Tester(Math.sqrt(2));
}
}
- nothing at all
-
hello
-
1.4142135623730951
-
hello
followed by good-bye
-
good-bye
followed by hello
Question # : 30. What is printed by the following program?
public class A {
public static void main(String[] args) {
int x = 1;
System.out.print(fun(x) + " ");
System.out.print(x + " ");
}
public static int fun(int x) {
x = x - 1;
return x;
}
}
-
1 0
-
1 1
-
0 1
-
0 0
- none of the above
Question # : 31. What is printed by the following program?
public class A {
public static void main(String[] args) {
int[] x = {1, 2, 3, 4, 5};
System.out.print(fun(x) + " ");
System.out.print(x[2] + " ");
}
public static int fun(int[] x) {
x[2] = x[2] - 1;
return x[2];
}
}
-
3 3
-
3 2
-
2 3
-
2 2
- none of the above
Question # : 32. Assume that x
is an integer variable that
appears in the following boolean
expression:
(x >= 14) && (x <= 12)
Which of the following is a simplification
of the expression?
-
x == 13
-
(x < 14) || (x > 12)
-
true
-
false
- none of the above
Question # : 33. Assume that x
is an integer
variable that appears in the following expression:
(x >= 12) || (x <= 14)
Which of the following is a simplification
of the expression?
-
x != 13
-
(x < 14) && (x > 12)
-
true
-
false
- none of the above
Question # : 34. What is printed by the following program?
public class A {
public static void main(String[] args) {
System.out.print(fun(5) + " ");
}
public static int fun(int x) {
System.out.print(x + " ");
return x + 1;
}
}
-
5
-
6
-
5 5
-
5 6
- none of the above
Question # : 35. What is printed by the following program fragment?
int x = 1, y = 1;
y += x;
x += y;
System.out.println(x + " " + y);
-
1 2
-
2 1
-
2 3
-
3 2
- none of the above
Question # : 36. Given the following definition
public static String mix(String s, String t) {
String ans = "";
int slen = s.length(), tlen = t.length();
for (int i = 0; i < slen && i < tlen; i++) {
ans = ans + t.charAt(i) + s.charAt(i);
}
return ans;
}
what does mix ("123", "45678")
evaluate to?
-
12345678
-
142536
-
415263
-
123456
- none of the above
Question # : 37. What is printed by the following program:
public class A {
public static void main(String[] args) {
int yo = 12;
System.out.println(yo(yo));
}
public static boolean yo (int yo) {
if (yo == 12)
return true;
return false;
}
}
-
true
-
false
- this code will not compile
- this code compiles but produces a run-time error
- none of the above
Question # : 38. What is printed by the following program:
public class A {
public static void main(String[] args) {
System.out.println(fun(1, fun(fun(2, fun(3, 4)), 5)));
}
public static int fun(int a, int b) {
return a + b;
}
}
-
5
-
12
-
15
-
17
-
25
Question # : 39. Is this an infinite loop?
for (int i = 0; i < 10; i--)
System.out.println(i);
- yes
- no
Question # : 40. Is this an infinite loop?
for (int i = 1; i != 10; i = i + 3)
System.out.println(i);
- yes
- no
Question # : 41. Given the following declaration, what does
a[a[a[a[0]]]]
evaluate to?
int[] a = {1, 2, 3, 4, 5};
-
1
-
2
-
3
-
4
-
5
Question # : 42. Given the following declaration, what does
a[1 + a[1 + a[0]]]
evaluate to?
int[] a = {1, 2, 3, 4, 5};
-
1
-
2
-
3
-
4
-
5
Question # : 43. Assume the following two arrays:
String[] name = {"One", "Two", "Three", "Four", "Five"};
boolean[] mark = {false, true, true, false, true};
And a method fun
with the following definition:
public static String fun(String[] a, boolean[] b) {
for (int i = 0; i < b.length; i++) {
if (b[i]) {
return a[i];
}
}
return "One";
}
What does fun(name, mark)
evaluate to?
-
One
-
Two
-
Three
-
Four
- none of the above
Question # : 44. Now assume that the arrays are like this:
String[] name = {"One", "Two", "Three", "Four", "Five"};
boolean[] mark = {false, false, false, false, false};
and the method still has the same definition:
public static String fun(String[] a, boolean[] b) {
for (int i = 0; i < b.length; i++) {
if (b[i]) {
return a[i];
}
}
return "One";
}
What does fun(name, mark)
evaluate to?
-
One
-
Two
-
Three
-
Four
- none of the above
Question # :45. Exactly how many question marks will appear on the screen
when the following program fragment is executed?
for (int x = 0; x < 3; x++) {
for (int y = -3; y < 0; y++) {
System.out.print("?");
}
}
-
4
-
6
-
9
-
16
- none of the above
Question # : 46. What does the following program fragment print?
int[] a = new int[5];
a[0] = 10;
for (int i = 1; i < a.length; i++)
a[i] = a[i-1]-1;
int value = a[0];
for (int i = 0; i < a.length; i++)
if (value > a[i])
value = a[i];
System.out.println(value);
-
3
-
0
-
4
-
1
- none of the above
Question # : 47. What does the following program print?
int[][] a = { {1, 2, 3, 4},
{2, 3, 4, 1},
{3, 4, 1, 2},
{4, 1, 2, 3}};
int sum = 0;
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[i].length; j++) {
if (a[i][j] % 2 == 0)
sum = sum + a[i][j];
}
}
System.out.println(sum);
-
6
-
12
-
20
-
24
- none of the above
Question # : 48. What does the following program print?
int[][] a = { {1, 2, 3, 4},
{2, 3, 4},
{3, 4},
{4}};
int sum = 0;
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[i].length; j++) {
if (a[i][j] % 2 == 0)
sum = sum + a[i][j];
}
}
System.out.println(sum);
-
6
-
12
-
20
-
24
- none of the above
Question # : 49. What does the following program print?
int[][] a = { {1, 2, 3, 4},
{2, 3, 4},
{3, 4},
{4}};
int sum = 0;
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[i].length; j++) {
if (a[i][j] % 2 == 0)
sum = sum + 1;
}
}
System.out.println(sum);
-
6
-
12
-
20
-
24
- none of the above