Java

TOPIC 18 – TEST II REVIEW

 

 

DETAILS & REVIEW

 

 

DETAILS

 

The test covers Unit 2.  All notes.  All work.  All class discussion is fair game.

Your test will be fully written.  You will not have a computer.

 

Minor programming errors will not count against you.

 

The Java template will be provided for you.  So you do not have to memorize the method prototype for the main function.

 

REVIEW

 

QUESTION 1

Define the following terms.

 

          Boolean variable    
          Boolean expression

          Control flow structure

          Condition
          Conditional operator

 

QUESTION 2

What do each of the following symbols stand for?

 

Symbol

Meaning

>

>=

<

<=

==

!=

&&
||
!

 

 

 

QUESTION 3

What will be outputted to screen?  There may be cases where nothing is outputted at all.

a)

int x = 0;
if (x >= 0)
{
   System.out.println("A");
}
System.out.println("B");

b)

int x = 0;
if (x >= 0)
{
   System.out.println("A");
}
else
{
   System.out.println("B");
}

c)

int x = 10;
if (x > 0)
{
   System.out.println("A");
}
else if (x > 5)
{
   System.out.println("B");
}

d)

int x = 10;
if (x < 0)
{
   System.out.println("A");
}
else if (x == 5)
{
   System.out.println("B");
}
System.out.println("C");

e)

int x = 10;
if (x > 0)
{
   System.out.println("A");
}
if (x > 5)
{
   System.out.println("B");
}
System.out.println("C");

f)

int x = 5;
if (x == 0)
{
   System.out.println("A");
}
else
{
   System.out.println("B");
}
if (x == 0)
{
   System.out.println("C");
}
else
{
   System.out.println("D");
}

g)

int x = 12;
if (x == 0)
{
   System.out.println("A");
}
else if (x < 100)
{
   System.out.println("B");
}
else if (x == 12)
{
   System.out.println("C");
}
else
{
   System.out.println("D");
}

h)

int coco = 25;
int nono = 43;
if (coco < 20 || nono < 10)
{
   System.out.println("A");
}
System.out.println("B");

i)

String s = "hello";
if (!s.equals("hi"))
{
   System.out.println("A");
}
else
{
   System.out.println("B");
}

j)

int coco = 25;
int nono = 43;
if (coco < 20 && nono < 10)
{
   System.out.println("A");
}
System.out.println("B");


QUESTION 4

Consider the following code and state the odds that the letter B will be outputted to screen.  A link to the solutions is at the bottom of this question.

a)

double rand = Math.random();
if (rand <= 0.40)
{
   System.out.println("A");
}
else
{
   System.out.println("B");
}

b)

double rand = Math.random();
if (rand <= 0.20)
{
   System.out.println("A");
}
else if (rand <= 0.50)
{
   System.out.println("B");
}
else
{
   System.out.println("C");
}

c)

double rand = Math.random();
if (rand <= 0.10)
{
   System.out.println("B");
}
else if (rand <= 0.50)
{
   System.out.println("A1");
}
else if (rand <= 0.80)
{
   System.out.println("A2");
}
else
{
   System.out.println("B");

}

d)

double rand1 = Math.random();
double rand2 = Math.random();
if (rand1 <= 0.50)
{
   if (rand2 > 0.40)
   {
      System.out.println("A");
   }
   else
   {
      System.out.println("B");
   }
}
else
{
   System.out.println("C");
}


Click here for solutions to this question.

QUESTION 5

Write a program that asks you for your favourite sports team.  It then gives a corresponding responses.  Half of the time, the response is positive and half of the time it is negative. 

There are two possible positive responses with the same odds of being chosen: "Go team go!" and "I love the team!".

There are three possible negative responses with the same odds of being chosen: "Boo team!", "Oh, I don't like the team!", "You really like the team? Why?".

Be careful to have the correct punctuation on all possible responses.  Note, the exclamation mark.

SAMPLE OUTPUT
What is your favourite sports team?
Senators

You really like the Senators? Why?


QUESTION 6

Write the program that asks the user for the current hour during the day.  It then asks if it is in the AM or PM.  Based on the provided information, the program outputs one of the following messages:

          11PM to 3AM – Get to bed!
          4AM to 6AM – Already up?
          7AM to 11AM – Good morning
          12PM to 5PM – Good afternoon
          6PM to 10PM – Good evening




 

Good luck!  May the force be with you.

 

 

J