Quiz Two Questions for Computer Programming I | CS 121, Quizzes of Computer Science

Material Type: Quiz; Class: Computer Programming I; Subject: Computer Science; University: Pace University-New York; Term: Unknown 1989;

Typology: Quizzes

Pre 2010

Uploaded on 08/09/2009

koofers-user-nvp-1
koofers-user-nvp-1 🇺🇸

9 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS121 Quiz 2
November 3, 2005
Name: _____________________________
1. Is the following code a valid Java program? If not, what is the problem? [10 points]
public class Test1 {
public static void main(String[] args) {
int x = 0;
if (x == 0) {
int x = 1;
System.out.println(x);
}
}
}
2. What will be printed by the following program? [10 points]
public class Test2 {
public static void main(String[] args) {
int x = 1;
switch (x) {
case 0:
System.out.println(x);
case 1:
System.out.println(x);
case 2:
System.out.println(x);
default:
System.out.println(x);
}
}
}
3. What is the function of the following program? [20 points]
public class Test3 {
public static void main(String[] args) {
if (args.length == 0)
System.exit(-1);
int left = 0, right = args.length – 1;
while (left < right) {
String temp = args[left];
pf2

Partial preview of the text

Download Quiz Two Questions for Computer Programming I | CS 121 and more Quizzes Computer Science in PDF only on Docsity!

CS121 Quiz 2

November 3, 2005

Name: _____________________________

  1. Is the following code a valid Java program? If not, what is the problem? [10 points]

public class Test1 { public static void main(String[] args) { int x = 0; if (x == 0) { int x = 1; System.out.println(x); } } }

  1. What will be printed by the following program? [10 points]

public class Test2 { public static void main(String[] args) { int x = 1; switch (x) { case 0: System.out.println(x); case 1: System.out.println(x); case 2: System.out.println(x); default: System.out.println(x); } } }

  1. What is the function of the following program? [20 points]

public class Test3 { public static void main(String[] args) { if (args.length == 0) System.exit(-1); int left = 0, right = args.length – 1; while (left < right) { String temp = args[left];

args[left] = args[right]; args[right] = temp; left++; right--; } for (int i = 0; i < args.length; i++) System.out.print(args[i] + " "); } }

  1. What is the relationship between the values in array a1[] and the values in array a2[][]? [20 points]

public class Test4 { public static void main(String[] args) { int[][] a2 = new int[4][4]; for (int i = 0; i < a2.length; i++) for (int j = 0; j < a2[0].length; j++) a2[i][j] = i + j; int[] a1 = new int[a2[0].length]; for (int i = 0; i < a2[0].length; i++) { a1[i] = 0; for (int j = 0; j < a2.length; j++) a1[i] = a1[i] + a2[j][i]; } for (int i = 0; i < a1.length; i++) System.out.print(a1[i] + " "); } }

  1. Write a concise and well-formatted Java program that [40 points] a. Creates an array holding integer values { 2, 4, 6, 8 10, 12, 15, 18, 25, 31}; b. Prompts the user to enter an integer for looking up (input should not be from a command-line argument); c. Uses a loop to implement sequential search of the input integer in the integer array created in step (a). If the input integer is not in the array, print -1. Otherwise print the index value for that value in the array.