Computer Science Term 1: Final
Which character is NOT allowed in a variable name?
&
Consider the following code: String major = "Computer Science"; What is returned by the method call major.charAt(1)?
'o'
Consider the following code: double list [] = new double [50]; The index of the first value is _______ and the last index is ______.
0, 49
Given the following method definition: public static mystery(int a[]) { int m = a[0]; for (int i = 0; i < a.length; i++) { if (m > a[i]) m = a[i]; } return m; } What would be returned by mystery if it was passed the following array? int a[] = {34, 18, 34, 38, 27, 37, 39, 21, 19};
18
What is output by the following code? int x = 36 % 8; if (x >= 10) System.out.println(1); else if (x >= 8) System.out.println(2); else if (x >= 6) System.out.println(3); else if ( x >= 4) System.out.println(4); else System.out.println(5);
4
What is output? int x = 9; System.out.println(x / 2);
4
What is output to the screen by the following code? int num = 1987; while (num > 0) { num = num / 10; System.out.print(num % 10 + " "); }
8 9 1 0
What is 0 1 0 1 1 1 0 1 in base ten?
93
Which of the following is a legal variable name in Java?
Ans
What is the best way to avoid round off error?
Convert doubles to ints and do calculations using integers
What is modular division NOT used for?
Converting decimals to whole numbers
What is a legal variable assignments in Java?
Double n=3;
What is the result of compiling and running the following code? int s = Math.sqrt(26);
Error: possible loss of precision
Consider the following variables and method representing a student: private int finalExam; private int cumulativeAverage; public boolean isPassing() { /* Missing Code */ } A student can pass a class if one of the following is true: ● The final exam is at least 98 ● The cumulative average is over 60 Which of the following correctly replaces /* Missing Code */ so that the method works as intended? I. if(finalExam >= 98 || cumulativeAverage > 60) return true; return false; II. boolean pass = false; if (finalExam >= 98) pass = true; if (cumulativeAverage > 60) pass = true; return pass; III. if(finalExam >= 98 && cumulativeAverage > 60) return true; return false;
I and II only
Of the following code blocks, which one correctly executes exactly two commands when the condition is true?
III only III. if (y == 99) { System.out.println("A"); System.out.println("B"); }
System.out.println("The answer is: " + 5 + 19);
The answer is 519
What mistake is in the following code? public static double mystery(double a) { System.out.println(a * 3.14); }
The double should be changed to void since the method does not return a value
The following loop is intended to print the even numbers from 20 to 26: int x = 20; while (x < 26){ System.out.print(x); x++; } What would you need to change in order for the code to work correctly?
The x++ needs to be x += 2 and the x < 26 needs to be <=
Assume that x and y are boolean variables and have been properly initialized. !(x || y) || (x || y) The result of evaluating the expression above is best described as:
always true
Consider the code: if ( y == 0 || x * y > 10) Which of the following is an example of short circuit evaluation?
if y == 0 is true it doesn't evaluate x * y > 10
Which of the following correctly gives random numbers between -10 and 10 inclusive?
int n = (int)(Math.random() * 21) - 10;
Consider the following code: String q = "power"; String r = "brown"; System.out.println( q.indexOf( r.charAt(3))); What is output?
2
int c = 3 - 37 % 2; System.out.println ( c );
2
11. What is output by the following code? System.out.print(21.0 / 5);
4.2
Consider the following code: int count = 4; while (count <= 7) { count++; System.out.print(count + " "); } What are the first and last numbers output?
5 8
How many times will the following loop repeat? int num = 49; while (num > 0) { if (num % 2 == 0) num++; else num--; }
Infinite Loop
double x = 7.4549;
Nothing wrong
When you pass an array to a method, the method receives ______.
a copy of the reference to the array
What is output by the following code? String q = "adjective"; String r = "stinky"; System.out.println( q.charAt( r.indexOf('t')));
d
Assume that x and y are boolean variables and have been properly initialized. (x && y) || !(x || y) Which of the following best describes the result of evaluating the expression above?
true only when x and y have the same value
Consider the following code, intended to count the number of words in the array with a length less than or equal to 6. String vocabulary [] = /* Array initialized with Strings */; int c = 0; for (int i = 0; i < vocabulary.length; i++) { if ( /* Missing Code */) c++; } System.out.println("Number of words with length less than or equal to 6: " + c); What could be used to replace /* Missing Code */ so that the code works as intended?
vocabulary[i].length() <= 6
Consider the following boolean statement: !( x >= y || w == z ) Which of the following will produce the same result?
x < y && w != z
!(x < y && w == z) is the same as which boolean expression?
x >= y || w != z
Consider the following method: static void nPrint(String a, int n) { System.out.println( a.charAt(n)); } What potential error could this method cause?
. Run time error - index out of range
What is (6 % 2) *7?
0
What is 45 in binary?
0 0 1 0 1 1 0 1
What is output by the following code? for (int i = 0; i <= 5; i++) { System.out.print(i + " "); }
0 1 2 3 4 5
Consider the following code: String words [] = {"avalanche", "budget", "cannot", "center", "meaning", "clear", "furniture", "deep", "piccolo", "friendly", "potatoes"}; int c = 0; for(int i = 0; i < words.length; i++) { if (words[i].substring(0,3).indexOf('o') >= 0) c++; } System.out.println( c ); What is output?
1
What is output to the screen by the following code? int c = 2; while (c < 6) { System.out.print((int)Math.pow (-1, c)+" "); c++; }
1 -1 1 -1
What is output by the following code? public static void stuff (int w) { w -= 2; } public static void main(String a[]) { int n = 2; stuff(n); System.out.print(n); }
2
Consider the following code: int [] a = {2, 6, 8, 10, 12, 14, 16, 18}; int sum = 0; for(int i = 0; i < a.length; i++) { if ( i%3 == 0 ) sum += a[i]; } System.out.println(sum); What is output?
28
Consider the following recursive method: public static int recur(int x) { if (x >= 0) return x + recur(x - 1); return 0; } What is returned by the method call recur(9)?
45
Assume the following method has been defined: public static int mystery(String a[], int x) { int c = 0; for (int i = 0; i < a.length; i++) if (a[i].length() > x) c++; return c; }
5
What is output by the following code? int a [] = {64, 66, 67, 37, 73, 70, 95, 52, 81, 82}; for (int i = 0; i < a.length; i++) { a[i] = a[i] / 10; } for (int i = 0; i < a.length; i++) { System.out.print(a[i] + " "); }
6 6 6 3 7 7 9 5 8 8
Consider the following methods: public static double average(int nums []) { int sum = 0; for (int i = 0; i < nums.length; i++) { sum += nums[i]; } return (1.0 * sum) / nums.length; } //average public static int [] mystery(String a []) { int temp [] = new int[a.length]; for (int i = 0; i < a.length; i++) { temp[i] = a[i].length(); } return temp; } //mystery What is output by running the following? String spelling [] = {"against", "forms", "belief", "government", "democratic", "movement", "understanding", "single", "followed", "scenario"}; System.out.println( average( mystery(spelling)));
8.1
double x = -97.6; System.out.println(Math.abs(x));
97.6
The following truth table matches which boolean condition? A B ________ 1 1 1 1 0 1 0 1 0 0 0 1
A || (!A && !B)
What does the following algorithm do? public static void mystery(int nums[]) { for (int i = 0; i < nums.length; i++) { if (nums[i] % 2 != 0) nums[i]++; } }
Changes all the values in the array to even numbers.
Correct the following code: char q = -82;
Char q = (char) Math.abs(-82);
Data types smallest to largest (memory used)
Char, int, double, string
Which method(s) would produce the following output if they were passed the parameter, "hamster"? hamster hamste hamst hams ham ha h I. public static void mystery(String wo) { System.out.println(wo); if (wo.length() > 1) mystery( wo.substring(0, wo.length() - 1)); } II. public static void mystery(String wo) { if (wo.length() > 1) mystery( wo.substring(0, wo.length() - 1)); System.out.println(wo); } III. public static void mystery(String wo) { if (wo.length() > 1) mystery( wo.substring( wo.length() - 1)); System.out.println(wo); }
I only
Which of the following would print the numbers: 32 54 76 98
I, II and III I. for(int t = 32; t <= 100; t += 22) System.out.print(t + " "); II. int t = 10; while (t < 90) { t += 22; System.out.print(t + " "); } III. int t = 32; while (t < 100) { System.out.print(t + " "); t += 22; }
Consider the following code: int diff = 0; if (Math.abs(num1 - num2) == (num1 - num2)) diff = num1 - num2; else if (Math.abs(num2 - num1) == (num2 - num1)) diff = num2 - num1; Which of the following will have the exact same result?
I, II, and III I. int diff = Math.abs(num1) - num2; II. int diff = Math.abs(num1 - num2); III. int diff = Math.abs(num2 - num1);
Consider the following code segment: int c = 1; while (c <= 10) { if (c % 3 == 1) System.out.print(c + " "); c++; } Which of the following produce the exact same output?
II and III only II. int c = 1; while (c <= 10) { System.out.print(c + " "); c += 3; } III. int c = 0; while (c <= 10) { c++; if ( c % 3 == 1) System.out.print(c + " "); }
The following if statement tests the rainfall in New York's Central Park during the months of June, July and August. if (low <= rain && rain <= high) System.out.println("Rainfall amount is normal."); else System.out.println("Rainfall amount is abnormal."); It could be replaced with:
II only if (rain >= low) { if (rain <= high) System.out.println("Rainfall amount is normal."); else System.out.println("Rainfall amount is abnormal."); } else System.out.println("Rainfall amount is abnormal.");
Which of the following would a while loop be best for?
Inputting numbers until the user enters -1.
What would properly create A and B as integer variables?
Int A, B;
The following code is intended to input three integers and print the average: System.out.println("Please enter three integers: "); int a = scan.nextInt(); int b = scan.nextInt(); int c = scan.nextInt(); System.out.println("The average is: " + 1.0 * a + b + c/3); What is a potential problem with the code as written?
It needs ( ) so the order of operations happens correctly.
What are if statements used for in programs?
Making decisions
What does the following code do? String w3 = "aardvark"; System.out.println(w3.charAt(w3.length()-2));
Prints the second to last letter in the String.
Consider the following code: String w = "Rapunzel"; for (int i = 0; i < w.length(); i++) { if ( i%4 != 2) System.out.print(w.charAt(i) + " "); } What is output?
R a u n z l
What does the following method do? public static int mystery(int a[], int x) { int c = 0; for ( int i = 0; i < a.length; i++) if (a[i] == x) c++; return c; }
Returns a count of the number of times x appears in the array
What does the String method charAt() do?
Returns the character at a certain location in the String object.
What does the following algorithm do? public static boolean mystery(int nums[]) { for (int i = 1; i < nums.length; i++) if (nums[i - 1] >= nums[i]) return false; return true; }
Returns true if each element of the array is greater than the element before
Which of the following is NOT a primitive data type?
String
Which of the following correctly inputs a String?
String Val= scan.nextLine();
Which correctly stores the word umbrella in a variable called stuff?
String stuff = "umbrella";
int val = 13.2; Does this need a cast to avoid a compiler error? If so, what should you type to cast?
Yes, (int)
Which of the following is used to indicate a new line?
\n
public static void printSport(double n) { System.out.print("football "); printSport((int)(n)); } public static void printSport(int n) { System.out.print("basketball "); }
basketball
The following is intended to count the number of times the number 87 is found in an array of test scores: int [] d = /* Assume array is initialized */; int scoreCount = 0; for (int i = 0; i < d.length; i++) { if (/* Missing Code */) scoreCount++; } System.out.println("Number of 87's: " + scoreCount); Which of the following could replace /* Missing Code */ so that the code works as intended?
d[i] == 87
Consider a method defined with the header: public static void doStuff(int x) Which of the following method calls is legal?
doStuff(9);
Which of the following needs a cast?
double stored in an int variable
Which if statement below tests if the variable letter holds the char value w?
if ( letter == 'w')
Which if statement below tests if letter holds R? (letter is a char variable)
if (letter == 'R')
Consider the code: if (a < b && c != d) Which of the following is an example of short circuit evaluation?
if a < b is false it doesn't evaluate c != d
What does short circuit evaluation mean in the following code? if ( a < b || c != d )
if a < b is true it doesn't evaluate c != d
The following is intended to return the location of the first instance of the String the user enters from the keyboard, -1 if not found. String names [] = new String[20]; //assume array is initialized System.out.println("Enter a name to search for: "); String lookingFor = scan.nextLine(); int found = -1; for (int i = 0; i < names.length; i++) { if (/* Missing Code */) { found = i; break; } } Which of the following could replace /* Missing Code */ so that it works as intended?
lookingFor.equals(names[i])
Consider the following code, intended to search an array for a value and print the position where that value was found: int [] array = /* Assume array is correctly initialized */; int num = /* Input from the keyboard */; int position = -1; for (int i = 0; i < array.length; i++) { if ( array[i] == num ) /* Missing Code */ } if (position == -1) System.out.println("Value not found"); else System.out.println("Value found at position " + position); What could replace /* Missing Code */ so that the code works as intended?
position = i;
What return statement may be used in p()? public static int[] p() { // ... }
return new int[]{1, 2, 3};