0% found this document useful (0 votes)
5 views27 pages

Java Compilation Exercises and Outputs

The document contains a series of questions and answers about basic programming concepts in Java such as variables, arithmetic operators, if/else conditionals, for loops, Strings, and I/O. Expressions are evaluated and predictions are requested for the outputs of small code snippets.

Translated by

ScribdTranslations
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views27 pages

Java Compilation Exercises and Outputs

The document contains a series of questions and answers about basic programming concepts in Java such as variables, arithmetic operators, if/else conditionals, for loops, Strings, and I/O. Expressions are evaluated and predictions are requested for the outputs of small code snippets.

Translated by

ScribdTranslations
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Question 1

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)

Suppose the following portion of code where there is a variable number defined previously. Evaluate the
execution with values 3, 12, 18, 20, 22.

What values would the variable result take in each evaluation?

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)

What is the output of the following program in JAVA?

boolean a = false;

boolean b = true;

boolean c = !a;

boolean d = a || b;
boolean e = d || c && !a;

boolean f = true && !e;

[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:

Enter the eclipse.


Create a project.
3. Create a class.
4. Write the header public static void main (String[] args){
5. Declare variables (IF NECESSARY)

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);
1

15

12

17

Correct!

15

11

17

15

10

17

15

11

17
Question 4
3.75/ 3.75 points.
Compilation Exercise:

Enter the eclipse.


2. Create a project.
3. Create a class.
4. Create the header public static void main (String[] args) {
5. Declare variables (IF NECESSARY)

What is the output of the following program in JAVA?

for (int i=1;i<=30;i+=2){


[Link]("the numbers are: " + i);
}
Correct!

the numbers are: 1


the numbers are: 3
the numbers are: 5
the numbers are: 7
the numbers are: 9
the numbers are: 11
the numbers are: 13
the numbers are: 15
the numbers are: 17
the numbers are: 19
the numbers are: 21
the numbers are: 23
the numbers are: 25
the numbers are: 27
the numbers are: 29

the numbers are: 12


the numbers are: 32
the numbers are: 52
the numbers are: 72
the numbers are: 92
the numbers are: 12
the numbers are: 13
the numbers are: 15
the numbers are: 14
the numbers are: 19
the numbers are: 18
the numbers are: 20
the numbers are: 21
the numbers are: 20
the numbers are: 22

the numbers are: 0


the numbers are: 31
the numbers are: 51
the numbers are: 71
the numbers are: 91
the numbers are: 111
the numbers are: 131
the numbers are: 151
the numbers are: 171
the numbers are: 191
the numbers are: 211
the numbers are: 231
the numbers are: 251
the numbers are: 271
the numbers are: 291

the numbers are: 2


the numbers are: 4
the numbers are: 6
the numbers are: 8
the numbers are: 10
the numbers are: 12
the numbers are: 14
the numbers are: 16
the numbers are: 18
the numbers are: 20
the numbers are: 22
the numbers are: 24
the numbers are: 26
the numbers are: 28
the numbers are: 30

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)

What is the output of the following program in JAVA?


intx=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](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)

What is the output of the following program in JAVA?

int i = 1, j = 2, k = 0;

double x = 2.1, y = 4.5, z = 10.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)

What is the output of the following program in JAVA?

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:

Enter the 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

String u = i + "" + n + j + "!";

[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:

Enter the eclipse.


2. Create a project.
3. Create a class.
4. Create the header public static void main (String[] args) {
5. Declare variables (IF NECESSARY)

What is the output of the following program in JAVA?

Considering that: a=28 b=11


resultado=a * b;
[Link]("The result is" + resultado);

500

30
Correct!

308

109

200
Question 14
3.75/ 3.75 points.
Compilation Exercise:

Enter the eclipse.


2. Create a project.
3. Create a class.
4. Write the header public static void main (String[] args) {
5. Declare variables (IF NECESSARY)

What is the output of the following program in JAVA?

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;

double x = 2.1, y = 4.5, z = 10.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

String u = i + "" + n + j + "!";

[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)

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);

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)

What is the output of the following program in JAVA?

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

String u = i + "" + n + j + "!";

[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!

Evaluation score: 75 out of 75


AnteriorSiguiente

You might also like