Java

TOPIC 27 – TEST REVIEW

 

 

DETAILS & REVIEW

 

 

DETAILS

 

The test covers Unit 3.  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.

 

            Iteration

            Loop

            Infinite loop

            Control variable

 

 

QUESTION 2

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

a)

int x = 0;
while (x > 3)
{
   System.out.println(x);
   x++;
}

b)

int y = 4;
while (y > 7)
{
   System.out.println("y");
   y++;
}

c)

int z = 10;
while (z > 0)
{
   if (z % 2 == 0)
   {
      System.out.println(z);
   }
}

d)

int a = 42;
if (a != 42)
{
   System.out.println("yo");
}
else
{
   while (a == 42)
   {
      System.out.println("ya");
      a++;
   }
}

e)

int a=4;
int b=3;
while (a + b < 10)
{
   System.out.println(a);
   a = a – 1;
   b = b + 2;
}

f)

int a=5;
int b=5;
while (10 – b == a)
{
   a++;
   b--;
   if (b == 2)
   {
      System.out.println("yoda");
   }
   else if (b == 1)
   {
      System.out.println("schmoda");
      b++;
   }
}

g)

for(int x=1; x<6; x++)
{
   System.out.println(x);
}

h)

for(int x=1; x<4; x++)
{
   int z = 10 – x;
   System.out.println(z);
}

i)

for(int x=1; x<8; x--)
{
   System.out.println(x);
}

j)

for(int x=1; x<31; x=x+5)
{
   System.out.println(x);
}

k)

for(int a=2; a<9; a++)
{
   if (10 % a == 0)
   {
      System.out.println(a);
   }
}

l)

for(int x=1; x<3; x++)
{
   for(int y=1; y<=2; y++)
   {
      System.out.println("clicky");
   }
}

m)

String s = "***";
for(int x=1; x<=4; x++)
{
   System.out.println(s+s);
}

n)

for(int x=1; x<=3; x++)
{
   for(int y=1; y<=3; y++)
   {
      System.out.println("***");
   }
}

o)

for(int x=1; x<=3; x++)
{
   for(int y=1; y<=4; y++)
   {
      System.out.print("*");
   }
   System.out.println("");
}

p)

for(int x=1; x<=5; x++)
{
   for(int y=1; y<=x; y++)
   {
      System.out.print("*");
   }
   System.out.println("");
}

q)

for(int x=1; x<=3; x++)
{
   if (x == 2)
   {
      System.out.print("o");
   }

   else if (x == 1)
   {
      System.out.print("camp");
   }
   else
   {
      System.out.println(" rocks");
   }
}

r)

for(int x=1; x<=5; x++)
{
   System.out.println("b");
   while(x <= 2)
   {
      if(x % 2 != 0)
      {
          System.out.println("a");
      }
      x++;
   }
}

 

QUESTION 3
a) Use the while loop to output all numbers from 1 to 15 on the screen.
b) Use the for loop to output all numbers from 1 to 15 on the screen.

QUESTION 4
Write a program that asks the user for an upper bound and a lower bound.  It then asks if the user wants the even numbers or the odd numbers and displays all such numbers that exist between the bounds.

Sample output:

     Enter a lower number.
     6
     Enter a higher number.
     15
     Do you want odd or even numbers?
     odd
     Here are all odd numbers between 6 and 15:
     7
     9
     11
     13
     15

QUESTION 5 (ADVANCED)

Write a program that uses nested for loops to output a diagonal line of * as shown below.  The number of stars should be provided by the user.

 

Sample output 1

 

How many stars?

4

 

*

 *

  *

   *

 

Sample output 2

 

How many stars?

7

 

*

 *

  *
   *

    *

     *

      *


 

Good luck!  May the force be with you.

 

 

J