Java Compilation Exercises and Outputs
Java Compilation Exercises and Outputs
1. Enter eclipse.
2. Create a project.
3. Create a class.
4. Create the header public static void main (String[] args){
5. Declare variables (IF NECESSARY)
Suppose the following portion of code where there is a variable number defined previously. Evaluate the
execution with values 3, 12, 18, 20, 22.
int resultado = 0;
if (number % 2 == 0) {
if (number % 4 == 0) {
if (number % 6 == 0)
resultado = 3;
else
resultado = 2;
else {
resultado = 1;
else {
resultado = 0;
Correct!
resultado = 0 paranumero = 3
resultado = 3 paranumero = 12
resultado = 1 paranumero = 18
resultado = 2 paranumero = 20
resultado = 1 paranumero = 22
resultado = 2 paranumero = 3
resultado = 3 paranumero = 12
resultado = 1 paranumero = 18
resultado = 2 paranumero = 20
resultado = 2 paranumero = 22
resultado = 1 paranumero = 3
resultado = 3 paranumero = 12
resultado = 3 paranumero = 18
resultado = 2 paranumero = 20
resultado = 2 paranumero = 22
resultado = 1 paranumero = 3
resultado = 3 paranumero = 12
resultado = 3 paranumero = 18
resultado = 1 paranumero = 20
resultado = 2 paranumero = 22
Question 2
3.75/ 3.75 points.
Compilation Exercise:
Enter eclipse.
2. Create a project.
3. Create a class.
4. Implement the header public static void main (String[] args) {
5. Declare variables (IF NECESSARY)
boolean a = false;
boolean b = true;
boolean c = !a;
boolean d = a || b;
boolean e = d || c && !a;
[Link] (a);
[Link](b);
[Link](c);
[Link] (d);
[Link] (e);
[Link](f);
false
true
true
true
true
true
false
true
false
true
true
false
false
true
true
true
false
false
Correct!
false
true
true
true
true
false
Question 3
3.75/ 3.75 points.
Compilation Exercise:
int a = 1;
int b = 2;
int c = 5;
int d = (a + b) * c;
int e = a + b * c;
int f = 2 * a * a + b * 3 + c;
[Link] (a);
[Link](b);
[Link](c);
[Link] (d);
[Link](e);
[Link](f);
1
15
12
17
Correct!
15
11
17
15
10
17
15
11
17
Question 4
3.75/ 3.75 points.
Compilation Exercise:
Question 5
3.75/ 3.75 points.
Which library should I call first to take input from the keyboard?
import [Link];
import [Link];
Correct!
import [Link];
import [Link];
Question 6
3.75/ 3.75 points.
What is the output of the following program in JAVA?
int a = 1;
int b = 2;
int c = 5;
int d = (a + b) * c;
int e = a + b * c;
int f = 2 * a * a + b * 3 + c;
[Link](a);
[Link](b);
[Link] (c);
[Link](d);
[Link](e);
[Link] (f);
15
10
17
15
11
17
Correct!
15
11
17
15
12
17
Question 7
3.75/ 3.75 points.
Combine the type of variable with the type of data it supports.
Correct!
bool
True/False
Correct!
Char
Characters
Correct!
Double
Decimals
Question 8
3.75/ 3.75 points.
Compilation Exercise:
1. Enter eclipse.
2. Create a project.
3. Create a class.
4. Create the header public static void main (String[] args) {
5. Declare variables (IF NECESSARY)
r+=x/10000;
x=x%10000;
r += x / 1000;
x=x%1000;
r += x / 100;
x = x % 100;
r += x / 10;
r += x % 10;
[Link](x);
Correct!
50
130
180
90
Question 9
3.75/ 3.75 points.
Compilation Exercise:
1. Enter eclipse.
2. Create a project.
3. Create a class.
4. Create the header public static void main (String[] args) {
5. Declare variables (IF NECESSARY)
int i = 1, j = 2, k = 0;
x = i / 2;
y *= 2;
z = y / 3.0;
i++;
j += k + 5;
j += k + 2;
k--;
[Link] (x);
[Link](y);
[Link] (z);
[Link] (i);
[Link] (j);
[Link] (k);
2.0
9.0
1.0
-2
Correct!
0.0
9.0
3.0
-1
1.0
9.0
1.0
-1
3.0
11.0
1.0
2
-2
Question 10
3.75/ 3.75 points.
Compilation Exercise:
1. Enter eclipse.
2. Create a project.
3. Create a class.
4. Create the header public static void main (String[] args){
5. Declare variables (IF NECESSARY)
int n=2;
for(int i = 1; i<=10; i++){
[Link](n + " * " + i + " = " + n*i);
}
Correct!
2*1=2
2*2=4
2*3=6
2*4=8
2 * 5 = 10
2 * 6 = 12
2 * 7 = 14
2 * 8 = 16
2 * 9 = 18
2 * 10 = 20
4*1=4
4*2=8
4 * 3 = 12
4 * 4 = 16
4 * 5 = 20
4 * 6 = 24
4 * 7 = 28
4 * 8 = 32
4 * 9 = 36
4 * 10 = 40
1*1=1
2*2=4
3*3=9
4 * 4 = 16
5 * 5 = 25
6 * 6 = 36
7 * 7 = 49
8 * 8 = 64
9 * 9 = 81
10 * 10 = 100
6*1=6
6 * 2 = 12
6 * 3 = 18
6 * 4 = 24
6 * 5 = 30
6 * 6 = 36
6 * 7 = 42
6 * 8 = 48
6 * 9 = 54
6 * 10 = 60
Question 11
3.75/ 3.75 points.
Compilation Exercise:
char i = 'd';
char j = 'e';
int n = 2;
String s = "d+e";
of
[Link] (i);
[Link] (j);
[Link](s);
[Link] (t);
[Link] (u);
of
of
d2!
d+e
of!
of
Correct!
d+e
of
d2e!
of
of
of!
Question 12
3.75/ 3.75 points.
What is the output of the following program in JAVA?
int a = 1;
int b = 2;
int c = 5;
int d = (a + b) * c;
int e = a + b * c;
int f = 2 * a * a + b * 3 + c;
[Link] (a);
[Link](b);
[Link] (c);
[Link](d);
[Link](e);
[Link](f);
¡Correcto!
15
11
17
15
11
17
15
12
17
1
15
10
17
Question 13
3.75/ 3.75 points.
Compilation Exercise:
500
30
Correct!
308
109
200
Question 14
3.75/ 3.75 points.
Compilation Exercise:
int x=26750,r=0;
r += x / 10000;
x=x%10000;
r += x / 1000;
x = x % 1000;
/*
r+=x/100;
x=x%100;
*/
r += x / 10;
r += x % 10;
[Link](r);
Correct!
83 (83..83)
23
33
Question 15
3.75/ 3.75 points.
What is the output of the following program in JAVA?
int i = 1, j = 2, k = 0;
x = i / 2;
y *= 2;
z = y / 3.0;
i++;
j += k + 5;
j += k + 2;
k--;
[Link] (x);
[Link](y);
[Link](z);
[Link](i);
[Link] (j);
[Link](k);
2.0
9.0
1.0
-2
3.0
11.0
1.0
-2
Correct!
0.0
9.0
3.0
-1
1.0
9.0
1.0
-1
Question 16
3.75/ 3.75 points.
What is the output of the following program in JAVA?
char i = 'd';
char j = 'e';
int n = 2;
String s = "d+e";
de
[Link](i);
[Link] (j);
[Link](s);
[Link](t);
[Link](u);
e
d+e
of!
of
Correct!
d+e
of
d2e!
of
of
of!
of
of
d2!
Question 17
3.75/ 3.75 points.
We call a boolean expression one that only has two values.
possible: true or false.
False
Correct!
True
Question 18
3.75/ 3.75ptos.
Compilation Exercise:
Enter eclipse.
2. Create a project.
3. Create a class.
4. Create the header public static void main (String[] args) {
5. Declare variables (IF NECESSARY)
int a = 1;
int b = 2;
int c = 5;
int d = (a + b) * c;
int e = a + b * c;
int f = 2 * a * a + b * 3 + c;
[Link] (a);
[Link](b);
[Link](c);
[Link](d);
[Link] (e);
[Link](f);
Correct!
1
2
15
11
17
15
11
17
15
12
17
15
10
17
Question 19
3.75/ 3.75 points.
Compilation Exercise:
1. Enter eclipse.
Create a project.
3. Create a class.
4. Create the header public static void main (String[] args) {
5. Declare variables (IF NECESSARY)
int r3=0;
int x=3;
float a=28.99f;
int b=6;
x=(int)a;
b--;
x = x % b * b % x - 6;
[Link](x);
-0.92135647
Correct!
9 (9..9)
34
Question 20
3.75/ 3.75 points.
Compilation Exercise:
1. Enter eclipse.
2. Create a project.
3. Create a class.
4. Implement the header public static void main (String[] args) {
5. Declare variables (IF NECESSARY)
What is the output of the following program in JAVA?
char i = 'd';
char j = 'e';
int n = 2;
String s = "d+e";
of
[Link](i);
[Link] (j);
[Link](s);
[Link](t);
[Link](u);
of
of
d2!
d+e
of!
of
Correct!
d+e
of
d2e!
of
of
of!