V22.0002 - Section 3 - Sample Midterm 1 Exam Solutions

(15 points)

1. Which symbol is the statement terminator in Java? Define and give an example.

2. Re-write y = y - 1 using a predecrement operator. ____________

3. What is the value of z after this statement has been excecuted and which data type should you use?

z = ( 3 + 2 * 25 % 5);

4. What is the value of z after this statement has been excecuted and which data type should you use?

z = 7 * 3 + 15 / 3 - (7 % 2);

5. Given: int x = 10; ... how many ways can you write a single statement which adds 1 to x and list each statement.

 

6. (5 points) Given

		int silly;

Which of the following causes a number to be placed in silly. (circle one)
a) System.out.println ( silly	) ;
b)  silly = JOptionPane.showInputDialog("Type your number") ;
c) System.out.println ( "silly"	) ;
d)  "silly" = JOptionPane.showInputDialog("Type your number") ;
e) silly = Integer.parseInt("123");


7. (5 points) Given:
_______ answer;
 ________ = JOptionPane.showInputDialog("Type your first number") ;



Fill in the blanks so that no errors occur.

(20 points)

8. Trace the variables in this program and write down the output. If the variable is not defined write "n/d".

1. public class SampleMidterm1 {
2. public static void main( String[] args )
3. {

4. int a = 10, b = 15, c = 3, d = 2, e = 1 ;

5. a = b * c;
6. e = (a + c) / d;
7. e++;
8. a *= 10;
9. d = d%c;

10. System.out.println("a is " + a + "\nb is " + b + "\nc is " + c + "\nd is " + d + "\ne is " + e);
11. System.exit( 0 );
12. }
13. }

Line
a
b
c
d
e
1          
2          
3          
4          
5          
6          
7          
8          
9          
10          
11          
12          
13          

9. What does the following print? (15 points)

public static void main( String[] args )
{
int rows, columns;

for (rows=1; rows<=5; rows++)
{
for (columns=1; columns<=rows; columns++)
{

if (columns%2 == 0)
System.out.print ("*");
else System.out.print("-");
}
System.out.print("\n");
}

System.out.print("\n");
System.exit( 0 );
}
}

10. Write a program to add up all of the integers which are evenly divisible by 3 from 3 to 30. Print out the sum. (20 points)

11. (20 points.) Write a program that prints the character for a given ASCII value read as input. Thus an input of 65 would display 'A'.