Set 03 Study Questions

1.  Go back to the loop questions from the previous week and implement them all using for-loops instead of while loops!

2.  What will the output be:

int x = 2, y = 7;

if (y < 1 && x++ < 7)

  System.out.println("hello");

System.out.println("x = " + x);


3.  What will the output be:

int x = 7;

int y = x++ + 3;

int z = 7 * --x + 4;

System.out.println("x = " + x + ", y = " + y + ", z = " + z);


4.  What will the output be:

int x = 5;

x += 7;

x -= 2;

x /= 2;

x *= 8;

x %= 11;

System.out.println(x);

   5.  Write a chart showing the precedence of all of the following operators:   = , < , ==, &&, ||, !=, ++, + (addition), +=, --, * 

  6.  If two operators occur in the same expression, and they are on the same level in the precedence chart (it is a “tie”), how do you decide which operator gets evaluated first?

7.  Using parentheses indicate the order in which each of the following expressions will be evaluated or state that the expression represents an invalid expression.  You may assume that all variables are of type int.

a.  x / y * z % w

b.  x ++ + y ++

c.  x + y + z – w % p * 2

d.  x < y || z > m && y <= 4 

 

8.  List as many reasons as you can for why it is so important to invest lots of time in the “design” of a large-scale software projectbefore the project is coded.  (5 reasons were listed in class.)

9.  Write psuedocode for the following program.   The program will read in strings that represent names of students.  It will keep prompting the user for more and more names until the user enters “STOP”.  After that, the program will display the number of names entered, the name that is first alphabetically, and the name that is last alphabetically.

10.  Write psuedocode for a program that computes the number of digits in an integer.  For example, if the user enters 1792, the output will be “4”.

11.  Write psuedocode for a program that reads a sequence of integer values and decides whether or not it is a decreasing sequence.  The program will first read in the number of values to process, followed by the values themselves.  The output will be “Yes” if the sequence is decreasing, and “No” otherwise.

12.  Which of the following code fragments are OK, and which will cause problems?

a.  int x = 52;

double y = x;

b.  double x = 14;

int y = x;

c.  int x = 7;

long y = x;

d.  long x = 17L;

short y = x;


13.  Show how to use “explicit casting” to force the troublesome examples in the previous question to work.