Docsity

Sample Final Exam Questions | Programming with Multimedia Objects | CS 17700, Exams of Computer Science

Toggle sidebar
bg1
CS177 Sample Final Exam Questions
May 4, 2009
Question 1. After the following code fragment, what value does bcontain?
double value = 3.141592;
int a = 3;
int b = (int)(value - a);
b=4/b;
a. 4
b. 28.250184
c. This program crashes because of a division-by-zero error.
d. 28
Question 2. What is the value of zafter the following code has been executed?
boolean x = true;
boolean y = false;
boolean z = !(((x ^ y) && (x || y)));
a. true
b. false
c. This code does not compile.
d. This program will crash at run-time due to the number of parentheses
used.
Question 3. What will the value of nbe after the following line of code is executed?
intn=((655%3)%2)%1;
a. 0
b. 1
c. 2
d. 109
1
pf3
pf4
pf5

Partial preview of the text

Download Sample Final Exam Questions | Programming with Multimedia Objects | CS 17700 and more Exams Computer Science in PDF only on Docsity!

CS177 Sample Final Exam Questions

May 4, 2009

Question 1. After the following code fragment, what value does b contain?

double value = 3.141592; int a = 3; int b = (int)(value - a); b = 4 / b;

a. 4 b. 28. c. This program crashes because of a division-by-zero error. d. 28

Question 2. What is the value of z after the following code has been executed?

boolean x = true; boolean y = false; boolean z = !(((x ^ y) && (x || y)));

a. true b. false c. This code does not compile. d. This program will crash at run-time due to the number of parentheses used.

Question 3. What will the value of n be after the following line of code is executed?

int n = ((655 % 3) % 2 ) % 1;

a. 0 b. 1 c. 2 d. 109

Question 4. What is the output of the following code?

int i = 14; if( i < 5 ) System.out.println("Too cold!"); else if( i > 10 ) System.out.println("Too hot!"); else System.out.println("Just right.); System.out.println("You are very lucky."); a. Too cold! b. Too hot! c. Just right. You are very lucky. d. Too hot! You are very lucky.

Question 5. What is the output of the following code?

int count = 0; for(int i = 0; i < 10; i++ ) { if( i % 3 == 0 ) count /= 3; else if( i % 2 == 0 ) count += 2; else count++; }

System.out.println(count); a. 1 b. 2 c. 3 d. 4

Question 6. What is the output of the following code?

int[] array = new int[10]; for(int i = 0; i < array.length; i++ ) array[i] = i;

for(int i = 0; i < array.length; i++ ) array[i] = array[i] + array[i + 1];

System.out.println(array[array.length - 1]);

a. 5 b. 9 c. 17 d. 33

Question 9. Consider the following class definition.

public class Psychodamage { private int insanity;

public Psychodamage( int value ) { insanity = value; }

public int getInsanity() { return insanity; }

public int micromanage() { insanity = insanity / 2; } }

What is the output of the following code?

Psychodamage psycho1 = new Psychodamage( 10 ); Psychodamage psycho2 = new Psychodamage( 30 ); psycho2 = psycho1; psycho1.micromanage(); System.out.println("Insanity: " + pyscho2.getInsanity());

a. Insanity: 5 b. Insanity: 10 c. Insanity: 15 d. Insanity: 30

Question 10. Which of the following inheritance relationships makes the most sense?

a. Robot extends Metal b. HardRock extends Metal c. Gold extends Metal d. Metal extends Uranium

Question 11. Let n indicate the input size of the problem. What is the running time of the following code fragment, expressed in Big Oh notation, using the tightest possible bound?

int value = 0; for( int i = 0; i < 3*n; i++ ) { for( int j = 0; j < 2; j++ ) for( int k = 0; k < n; k++ ) value++; }

a. O(log n) b. O(n) c. O(n^2 ) d. O(n^3 )

Question 12. What does the following method do?

public static boolean unknown(double[] array) { for( int i = 0; i < array.length - 1; i++ ) { if( array[i] > array[i + 1] ) return false; } return true; }

a. Returns true if none of the adjacent elements in the array are the same and false otherwise. b. Returns true if the array is sorted in ascending order and false otherwise. c. Returns true if the array is sorted in descending order and false otherwise. d. This method will cause a run-time error.

Question 13. Given an object called picture of the Picture class, what is the relation- ship between picture and changed after the following code?

Picture changed = new Picture( picture.height(), picture.width() ); for( int i = 0; i < picture.width(); i++ ) for( int j = 0; j < picture.height(); j++ ) changed.set( j, changed.height() - i - 1, picture.get( i, j ) );

a. The image in changed is the image in picture flipped vertically.