Vous êtes sur la page 1sur 2

NAME ________________ STUDENT NUMBER__________ LAB GP__________

ECS401: Progress Test- Practice for mid term test


These exercises are to help us judge your progress. It is really important that you
make sure you fully understand everything we have covered so far.

Give the text that is printed to the screen after each fragment of code is executed.
1.
String n1 = "56";
String n2 = "12";
String a = n1 + " - " + n2;
System.out.println(a);
________________________________
2.
int z = 2;
int m = 5;
z = z + m;
z = z + 1;
System.out.println(z);
____________________________
3.
int x = 2;
if (x >= 0)
{ System.out.println(“1”); }
else if (x == 2)
{ System.out.println(“2”); }
else
{ System.out.println(“3”); }

4.
int x = 4;
if (x < 2)
{ System.out.println(“1”); }
else if (x == 2)
{ System.out.println(“2”); }
else
{ System.out.println(“3”); }

5.
int z = 10;
for(int j = 1; j <= 4; j++)
{
z = z + 1;
}
System.out.println(z);
____________________________
6.
int z = 0;
for(int j = 1; j <= 2; j++)
{
for(int k = 1; k <= 3; k++)
{
z = z + 1;
}
}
System.out.println(z); ____________________________
NAME ________________ STUDENT NUMBER__________ LAB GP__________

SPEND NO MORE THAN 20 MINUTES ANSWERING THIS QUESTION

7. On paper write a program to repeatedly give messages about the afternoon


weather. It should ask for the temperature at 1pm, 2pm, 3pm etc up to 6pm. For each
time it should print out a message about the temperature given.

However the messages should not be printed until all the temperatures have been
input. HINT: Build up the string to print in a variable as you process each input.

Temperatures above 40 or below -25 should lead to the message


“I don’t believe you!”
Temperatures between 20 and 39 should lead to the message
“What a scorcher!”
Temperatures between 10 and 19 should lead to the message
“Boring!”
Temperatures between -25 and 9 should lead to the message
“Chilly! Put your thermals on!”
In all cases this message should be preceded by the time and temperature as below.

An example run of the program might therefore be:

What is the temperature at 1pm? -10


What is the temperature at 2pm? 100
What is the temperature at 3pm? 15
What is the temperature at 4pm? 32
What is the temperature at 5pm? 20
What is the temperature at 6pm? 5

1pm: -10 degrees. Chilly! Put your thermals on!


2pm: 100 degrees. I don’t believe you!
3pm: 15 degrees. Boring!
4pm: 32 degrees. What a scorcher!
5pm: 20 degrees. What a scorcher!
6pm: 5 degrees. Chilly! Put your thermals on!

Vous aimerez peut-être aussi