CECS 174 Practice Exam #1

Multiple Choice (questions 1 to 12)

____ 1. Which of the following is not a valid Java identifier?
a. Stop_and_go
b. salaryForTheMonth
c. Hello_There!
d. myCounter
 

____ 2. Which of the following is a reserved word in Java?
a. char
b. Char
c. CHAR
d. None of these
 

____ 3. The value of the expression 17 % 7 is:
a. 1
b. 2
c. 3
d. None of these
 

____ 4. The value of the expression 36 – 15 % 2.0 + 1 is:
a. 30
b. 31
c. 37
d. This is an illegal Java expression
 

____ 5. The value of the Java expression 3 + 16 / 7 % 2 is:
a. 1
b. 3
c. 19
d. None of these

____ 6. Given

int one;
double two;
bool four;

which of the following assignment statements are valid?
(i) one = 7 * 3 % 4;
(ii) 2.3 + 3.5 = two;
(iii) four = (2 <= 3);

a. Only (i) is valid
b. (i) and (ii) are valid
c. (ii) and (iii) are valid
d. (i) and (iii) are valid
 

____ 7. What is the output of the following Java statement?

System.out.printl("Sunny "+ "\n"+"Day ");

a. Sunny \nDay
b. Sunny \nDay endl
c. Sunny
   Day
d. None of these 
 

____ 8. What is the output of the following Java code?

num = 10;
while(num > 10)
num = num - 2;
System.out.println(num);

a. 10
b. 8
c. 0
d. None of these
 

____ 9. What is the output of the following Java code?

num = 0;
while(num < 5)
{
System.out.println(num);
num = num + 1;
}

a. 0 1 2 3 4 5
b. 1 2 3 4 5
c. 0 1 2 3 4
d. None of these
 

____ 10. What is the value of counter after the following statements executes?

counter = 0;
while(counter <= 50)
counter = counter + 3;

a. 48
b. 50
c. 51
d. None of these
 

____ 11. Suppose sum and num are int variables, and the input is

11 35 28 -1

What is the output of the following code?

String s;
int num, sum =0;
s = JOptionPane.showInputDialog("Enter an integer number");
num =Integer.parseInt(s);
while(num != -1)
{sum = sum + num;
 s = JOptionPane.showInputDialog("Enter an integer number");
 num =Integer.parseInt(s);
}
System.out.println(num);

a. 74
b. 73
c. 53
d. 52
 

____ 12. Which of the following is true about a while loop?
a. The body of the loop is executed at least once.
b. The logical expression controlling the loop is evaluated before the loop is entered and after the loop exists.
c. The body of the loop may not execute at all.
d. None of these 

Answer

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

Variable naming

.1.  Which of the following, if any, are legal identifiers for Java?
     a) return
     b) 3X
     c) data
     d) %change
     e) prog.java

Answer

            Only (c) is a correct identifier. invalid identifiers are:
            a   key word
            b: starts with a digit
           d,e have characters other than digit, letter, or underscore.
           Use of the '.' and '%' characters in identifiers is illegal.

Java arithmetic evaluation

1) What is the answer to the following arithmetic operations?

            a) (1/5) * 3
            b) (8/2) * 4
            c) (1.0/5.0) * 3

Answer  Integer dividing by integer is always truncated.

a.  0
b. 14
c. 0.6

Is there any difference between a and c?  

Yes.   a.   1/5  -->  0
          c.   1.0/5.0  -->  0.2

2. Evaluate the following C++ expressions, where x =2, y=3, z = - 4, and w=5.

a.   x + z * y / w
     2 + (-4)*3/5 = 2-12/5 = 2 - 2 = 0
Answer    0

b.   w * x % y - 4
Answer

       5*2%3-4 =  10%3-4 = 1 - 4 = -3
Answer  -3

c.  14 % w % y
Answer

     14%5%3 = 4%3= 1

3. In questions a and b assume integer variables x=10, y=5, z=3, w=2.
Each question is independent. Circle the correct answer.

a. The value of x + y % w is
a. 1
b. 11
c. 12
d. none of the above

Answer 10 + 5%2 = 10 + 1 = 11

b. The value of x / z * y is
a. 16 2/3
b. 0
c. 15
d. none of the above

Answer 10/3*5 = 3*5=15

4. A student has written a Java program to convert temperature in degrees Celsius to degrees Fahrenheit. The following lines occur in this program. (This code is embedded in a correct program. )
          //...
         
double C, F;
                      String s;
                      s = JOptionPane.showInputDialog(“Enter Celsius:”);
                     C = Double.parseDouble(s);
                     F = (9/5)*C + 32.0;
                     System.out.println(F); 

a) What is the output for C = 20 degrees Celsius?
b)  Identify and explain the trouble on the line F = …….
c)  Give the corrected version of the erroneous line.

Answer 

     a. The output is 52.0.
     b. The problem is the multiplying factor, which evaluates, using int arithmetic, to 1.
     c. F = (9.0/5)*C + 32.0;

Java arithmetic expressions

1. Convert the following mathematical expressions to C++ expressions:
            a) 4x
            b) (2x + y)/3y
            c) (2x - 3y)/(z -1)
Answer

 a. 4*x
     b. (2*x -3*y)/3*y
     c. (2*x-3*y)/(z-1)

2. Write a Java expression for the following statement where a, b and c are variables.

-b + (b200 - 4ac)
______________

             2a

Answer (-b + (Math.pow(b,200.0) - 4*a*c))/ 2*a

3.  Convert the following algebra expressions into Java expressions
a) x2+3x-4
b) (x+y)z
c) x+3y / 2x-y
d) 1 / x2+x+3
e) x+y / 7

Answer

a) x2+3x-4 
x*2+3*-4
b) (x+y)z
(x+y)*z
c) x+3y / 2x-y
(x+3*y)/(2*x-y)
d) 1 / x2+x+3
1/(x2+x+3)
e) x+y / 7
x+y/7

4. Convert the following algebra expressions into Java expressions
a) x10+3x-4
b) x+3y
   2x-y
c) x2+x+3
   x+ yz
d) xy / 7

Answer

.     a. Math.pow(x,10) + 3*x -4      d. (x*x + x + 3)/(x + y*z)

c. (x + 3*y) / (2*x - y)   e. Math.pow(x,y) / 7      

while loop

1. Write a program segment that adds up the even integers from 16 through 2000, inclusive.

Answer

nt sum =0 , evenint = 16;
while ( evenint < = 2000)
{ sum + = evenint;
eventint + = 2;
}
2. Here is a collection of while statements. Identify: 
i.   those that are correct, and are likely to give the programmers intent;
ii.  those that are correct, but unlikely to give the programmer's intent, and
iii. what compiler error will the rest generate?
Assume all variables have been declared, but not necessarily initialized.
a)
 s = JOptionPane.showInputDialog(“Enter a number”);
 n = Integer.parseInt(s);
while (-1 != n)
{
sum = 0; 
sum = sum + n;
}
b)
s = JOptionPane.showInputDialog(“Enter a number”);
value = Integer.parseInt(s);              
while ( value != -1 )          
sum = sum + value;               
cin >> value;                    

c)
s = JOptionPane.showInputDialog(“Enter a number”);
n = Integer.parseInt(s);                     
int i = 1,                         
while ( i < n );              
i++;   

Answer
                        
a)
 s = JOptionPane.showInputDialog(“Enter a number”);
 n = Integer.parseInt(s);
while (-1 != n)
{
sum = 0; //Put sum = 0 above the while loop
sum = sum + n;
}
b)
s = JOptionPane.showInputDialog(“Enter a number”);
value = Integer.parseInt(s);                                                   

while ( value != -1 )          
sum = sum + value;           //  put { before sum
cin >> value;                //put } after value;

c)
s = JOptionPane.showInputDialog(“Enter a number”);
n = Integer.parseInt(s);                     
int i = 1,                          //change , to ;
while ( i < n );                   //no semicolon 
i++;                               //this while loop only increments i by one

3. Write a complete Java  program that counts the number of integers greater than 100 entered by the user. The user terminates the input with a negative number.
Answer

import javax.swing.*;
public class Test
{
  public static void main (String []args)
 {
int number, count =0;
String input;
System.out.println("Enter nonnegative numbers.");
System.out.println("Enter a negative value to terminate the input.");
input = JOptionPane.showInputDialog("Enter the first integer:”);
number = Integer.parseInt(input);

while (number >= 0)
{
    if (number > 100) 
         count++;
   input = JOptionPane.showInputDialog("Enter the next integer number:”);
  number = Integer.parseInt(input);
}
System.out.println("The count is " + count);

}

One-dimensional array and for loop

1. Declare a one-dimensional array named A of type integer.
Answer

int [] A ;

2. Create a one-dimensional array A in question 1 with 5 elements
Answer

A = new int[55];

3. Declare and create a one-dimensional array called B with 5 elements of type integer.
Answer

int [] B = new int[5];

4. Declare and initialize a one-dimensional array called C with the following values:

10,20,60 and 40.

Answer

int [] C = {10,20,60,40};

5. Multiple Choice

_A_   1.   Suppose you have the following declaration: 

double salesData[] = new double[1000]; 

Which of the following ranges is valid for the index of the array salesData

(i)  0 through 999
(ii) 1 through 1000 

a.

Only (i)

b.

Only (ii)

c.

Both (i) and (ii)

d.

None of these

 

_B_  2. .   Consider the following declaration:

       int list[] = new int[10];

   int j; 

      Which of the following correctly outputs all the elements of  list?

      (i)

            for(j = 1; j < 10; j++)
         System.out.println(list[j]+" ");
     System.out.println(""); 

      (ii)

            for(j = 0; j <= 9; j++)
         
System.out.println(list[j]+" ");
     System.out.println("");
 

a.

Only (i)

b.

Only (ii)

c.

Both (i) and (ii)

d.

None of these

 

_D_  3. .   Consider the following declaration:

 

double sales[]= new double[50];

int j; 

Which of the following correctly initializes the array  sales to 0?

 

(i)

            for(j = 0; j < 49; j++)
         sales[j] = 0;

 (ii)

            for(j = 1; j <= 50; j++)
         sales[j] = 0; 

a.

Only (i)

b.

Only (ii)

c.

Both (i) and (ii)

d.

None of these

 

_C_  4.   Consider the following declaration:

 

int list[] = new int[10];
int j;
int sum;
 

Which of the following correctly finds the sum of the elements of list

(i)

            sum = 0;
           
for(j = 0; j < 10; j++)
         sum = sum + list[j];  

(ii)

            sum = list[0];
           
for(j = 1; j < 10; j++)
         sum = sum + list[j]; 

a.

Only (i)

b.

Only (ii)

c.

Both (i) and (ii)

d.

None of these

for LOOPs

7. How many times is "Hi" printed to the screen
for(int i=0;i<14;i++);
   System.out.println("Hi");
a. 13
b. 15
c. 14
d. 1

Answer: D

8. Given the following code, what is the final value of i?
int i;
for(i=0; i<=4;i++)
{
     System.out.println( i );
}
a. 3
b. 4
c. 5
d. 0
Answer C

9. What is wrong with the following code fragment?
double scores[] = new score[50];
String s;
for(int i=0; i<=scores.length;i++)
{
s = JOptionPane.showInputDialog("Enter a score");
scores[i] =Double.parseDouble(s);
}
a. Array indexes start at 1 not 0
b. Arrays must be integers
c. Array indexes must be less than the size of the array
d. Should be cin >> scores[0];
 

6. Write the Java statement to perform the following tasks:

Declare and create a one-dimensional array named A with 5 elements of type integer
Declare and initialize a one-dimensional array called B with the following values: 10, 20, 60, 40 and 80.
Copy array B onto array A

Answer

int [] A = new int[5];
int [] B = { 10, 20, 60, 40 ,80};
for(int i = 0; i <A.length; i++)
   A[i] = B[i];

For problems on the class Pixel and Problems, please review the lecture notes,  lab assignments and homework.