Vous êtes sur la page 1sur 3

Write the output of the following Java code. Assumed that the code will run in a complete program.

(5pts. Each)
1.
for ( N = 3; N <= 36; N = N + 3 ) {
System.out.println( N );
}
2.
public static void main(String[] args) {
int N;
N = 1;
while (N <= 32) {
N = 2 * N;
System.out.println(N);
}
}
3.
public static void main(String[] args) {
int x,y;
x = 5;
y = 1;
while (x > 0) {
x = x - 1;
y = y * x;
System.out.println(y);
}
}
4.
class ForTest {
public static void main(String args[]) {
int x;
for (x = 0; x < 10; x = x + 1) {
System.out.println("This is x: " + x);
}
}
}
5.
class Nested {
public static void main(String args[]) {
int i, j;
for (i = 0; i < 10; i++) {
for (j = i; j < 10; j++) {
System.out.print(".");
}
System.out.println();
}
}

6.
class selection_statements {
public static void main(String args[])
{
int var1 = 5;
int var2 = 6;
if ((var2 = 1) == var1)
System.out.print(var2);
else
System.out.print(var2++);
}
}

Hint:
a) 1
b) 2
c) 3
d) 4
7.
class comma_operator {
public static void main(String args[])
{
int sum = 0;
for (int i = 0, j = 0; i < 5 & j < 5; ++i, j = i + 1)
sum += i;
System.out.println(sum);
}
}
Hint:
a) 5
b) 6
c) 14
d) compilation error
8.
class jump_statments {
public static void main(String args[])
{
int x = 2;
int y = 0;
for ( ; y < 10; ++y) {
if (y % x == 0)
continue;
else if (y == 8)
break;
else
System.out.print(y + " ");
}
}
}
Hint:
a) 1 3 5 7
b) 2 4 6 8
c) 1 3 5 7 9
d) 1 2 3 4 5 6 7 8 9

9.
class Output {
public static void main(String args[])
{
int x, y = 1;
x = 10;
if (x != 10 && x / 0 == 0)
System.out.println(y);
else
System.out.println(++y);
}
}

Hint:

a) 1
b) 2
c) Runtime error owing to division by zero in if condition.
d) Unpredictable behavior of program.

10.
class Output {
public static void main(String args[])
{
int a = 5;
int b = 10;
first: {
second: {
third: {
if (a == b >> 1)
break second;
}
System.out.println(a);
}
System.out.println(b);
}
}
}
Hint:

a) 5 10
b) 10 5
c) 5
d) 10

Vous aimerez peut-être aussi