CS 0007 - Practice Midterm
1.) Write a for loop that iterates 100 times.
2.) What does this code display on the screen if run?
int q = 5;
int r;
int s;
r = q++;
s = ++q;
q *= r + s;
q %= 10;
System.out.println(“q is ” + q);
System.out.println(“r is ” + r);
System.out.println(“s is ” + s);
3.) What is an algorithm and why is it important in Computer Science?
4.) What is a comment? How do you specify a line comment? A block comment? Describe when you should use a comment.
5.) In the following code, what is displayed if the user enters the number 3?
import tio.*
class Test { int input = Console.in.readInt();
int output = 1;
switch(input) {
case 2: output = 3; break;
case 3: output = 4; case 4: output = input + 5;
case 5: output = output + 1; break;
default: System.out.println(“Input unknown”); }
System.out.println(“The output is ” + output); } |
6.) What is the most appropriate data type to represent the following quantities?
a) Your grade point average
b) How many students go to Pitt
c) Your middle initial
d) A city name
e) Pi (p)
7.) Write a program to display a 5 x 5 multiplication table like so:
1 2 3 4 5
2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
5 10 15 20 25