CS Midterm 2
What is the output of this program? -------
0 2 4 6 8
What is the numerical range of a char?
0 to 65535
What is the output of the following program? public static void main(String args []) { int arr [ ] = new int [ ] {0,1,2,3,4,5,6,7,8,9}; int n = 6; n = arr[arr[n]/2]; System.out.println(arr[n]/2);
1
Which two form part of a correct array declaration?
1. public int a [] 2. static int [] a
Given the following code fragment: What is the value of A [3]?
10
What is the output of this program? let arr []= new int[-][]; arr [0] = new int [1] ; arr [1] = new int [2] ; arr [2] = new int [3];
10
What will happen when following Java code block is compiled and run? int ary[] = new int []{1,2,3}; System.out.println(ary[1]);
2
What is the output of the following program? static int fun( int 1) { return ++1; } public static void main(String [ ] args) { int i = 0; int j = func(i++) + 1 + func(++1); System.out.println(i); System.out.println(j);
2 5
What is the output of this program? double a,b; a = 3.0 b = 4.0 double c = Math.sqrt(a*a + b*b);
5
What will be the output of this program?
51
What will be printed? int [] a = {0,1,2,3,4,5,6,7}; System.out.println(a.length);
8
What is a variable?
A place to store information in a program
What will happen when following Java code block is compiled and run? public static void main(String argv[]) { int [] iArray = new int [10]; iArray.length = 15; System.out.println(iArray.length);
Compilation Error
What will be the output of this program? int I = 0; while(1) { if(I==4) { break; } ++1;
Compilation error
What is the process of fixing errors called?
Debugging
Which statement is true for the following code int odd = 1; if(odd) { System.out.println("odd"); } else { System.out.println("even");
Fails
What types of information are arrays best suited for?
Lists
Will the program compile successfully? public static void main(String[] args) { int arr1[] = new int[2]{1,2}; int arr2[] = new int [ ]{1,2,3}; } }
No
What will be the output to the program? int I = 1; do while (I < 1) System.out.print("I is" + I); While (I > 1);
Nothing will print
Which of these is necessary to specify at the time of one dimensional array initialization?
Row
What is a valid declarations of a String?
String s1 = null;
In JAVA, the arithmetic operator "%" is applied to compute:
The remainder of a division operation
Which of the following character will put a single quote in a string?
\ '
11. Which three are legal array declarations? 1.) Int [] myScores []; 2.) Char [] myChars; 3.) Int [6] myScores; 4.) Dog myDogs []; 5.) Dog myDogs [7]; a. 1, 2, and 4 b. 2, 4, and 5 c. 2, 3, and 4 d. All are legal e. None are legal
a. 1, 2, and 4
7. Which two are acceptable types for x? switch(x) { default: System.out.println("Hello"); } byte long char float short long a. Byte and Char b. Long and Float c. Char and Short d. Float and Long e. Long and Short
a. Byte and Char
18. What types of information are arrays best suited for? a. Lists b. Pairs of related information c. Trivia questions d. Looping thru variables e. Mixing related information associated with ints, doubles and strings
a. Lists
13. Which is a valid declarations of a String? a. String s1 = null; b. String s2 = 'null'; c. String s3 = (String) 'abc'; d. String s4 = (String) '\ufeed'; e. String s5 == 123abc;
a. String s1 = null;
Which of these is an incorrect statement? a. The only way to initialize an array is use the new operator. b. Array can be initialized when they are declared c. Array can be initialized using comma separated expression d. Mr. Jacks is the best teacher in the whole wide world
a. The only way to initialize an array is use the new operator.
19. Which statement causes a program to go back to the statement that began a loop and then keep going from there? a. continue b. next c. skip d. break e. stop
a. continue
3. Which Data Type is most often associated with Text Processing: a. string b. double c. int d. boolean e. Both B and C
a. string
Given a one DIM array arr, what is the correct way of getting the number of elements in arr.
arr.length
16. Will the following program compile successfully? class MyArray { public static void main(String[] args) { int arr1[]=new int[2]{1,2}; int arr2[]=new int[]{1,2,3}; } } a. Yes b. No c. Don't Answer C d. Don't Answer D e. Don't Answer E
b. No
8. Which statement is true for the following code: public class While { // LINE 1 public static void main(String args[]) { // LINE 2 public void loop() // LINE 3 { // LINE 4 int x= 0; // LINE 5 while ( 1 ) // LINE 6 { // LINE 7 System.out.print("x plus one is " + (x + 1)); // LINE 8 } // LINE 9 } // LINE 10 } // LINE 11 } // LINE 12 a. There is a syntax error on line 1 only. b. There is a syntax error on line 6 only. c. There are syntax errors on lines 1 and 6. d. There are syntax errors on lines 1, 6, and 8. e. There are syntax errors on lines 1, 5, 6, and 8.
b. There is a syntax error on line 6 only
9. Which one of these lists contains only Java programming language keywords? a. class, if, void, long, int, continue, dog b. class, do, while, else, for, if, switch, void, boolean, int c. try, virtual, throw, final, volatile, transient, cat d. strictfp, constant, super, implements, do, hamster e. byte, break, assert, switch, include, python
b. class, do, while, else, for, if, switch, void, boolean, int
15. Which cause a compiler error? a. int[ ] scores = {3, 5, 7}; b. int [ ][ ] scores = {2,7,6}, {9,3,45}; c. String cats[ ] = {"Fluffy", "Spot", "Zeus"}; d. boolean results[ ] = new boolean [] {true, false, true}; e. Integer results[ ] = {new Integer(3), new Integer(5), new Integer(8)};
b. int [ ][ ] scores = {2,7,6}, {9,3,45};
12. Which one of the following will declare an array and initialize it with five numbers not all "0"? a. Array a = new Array(5); b. int [] a = {23,22,21,20,19}; c. int a [] = new int[5]; d. int [5] array; e. none of the choices will declare an array and initialize it.
b. int [] a = {23,22,21,20,19};
Which one is a valid declaration of a boolean?
boolean b3 = false
14. Which two form part of correct array declarations? 1.) public int a [ ] 2.) static int [ ] a 3.) public [ ] int a 4.) private int a [3] 5.) private int [3] a [ ] a. 1, 3 b. 2, 4 c. 1, 2 d. 2, 5 e. 4, 5
c. 1, 2
20. What is a variable? a. omments in a program that is used by a developer to read the program b. Text in a program that is ignored by the compiler c. A place to store information in a program d. An array setup e. Something that wobbles but does not fall down
c. A place to store information in a program
public class Test2Ans08Output { public static void main(String args[]) { // YOU NEED TO SET 'a' and 'b' to the 4 possible combinations indicated in answers boolean a = ????; boolean b = ????; if( a ) { System.out.println("A"); } else if(a && b) { System.out.println( "A && B"); } else { if ( !b ) { System.out.println( "notB") ; } else { System.out.println( "ELSE" ) ; } } } a. If a is true and b is true then the output is "A && B" b. If a is true and b is false then the output is "notB" c. If a is false and b is true then the output is "ELSE" d. If a is false and b is false then the output is "ELSE" e. None of the above
c. If a is false and b is true then the output is "ELSE"
1. Which of the following is NOT a valid Identifier: a. a b. sales c. sales&profit d. TAX_RATE e. Intx
c. sales&profit
Which causes a program to go back to the statement that began a loop and then go from there?
continue
2. Modulus operator, %, can be applied to which of these? a. Integers b. Floating point numbers c. Boolean d. Both A and B e. None of the above
d. Both A and B
4. What happens if I forget to initialize a variable: a. The complier checks for this condition and if the variable is an int, it will be set to 0. b. The complier checks for this condition and if the variable is a char, it will be set to A. c. The complier checks for this condition and if the variable is a double, it will be set to 0.0 d. The complier checks for this condition and give you a "variable might not have been initialized" message e. Nothing happens, the compiler does not care
d. The complier checks for this condition and give you a "variable might not have been initialized" message
17. Which of these is an incorrect array declaration? a. int arr[] = new int[5] b. int [] arr = new int[5] c. int arr[] arr = new int[5] d. int arr[] = int [5] new
d. int arr[] = int [5] new
10. Which will legally declare, construct, and initialize an array? a. int [] myList = {"1", "2", "3"}; b. int [] myList = (5, 8, 2); c. int myList [] [] = {4,9,7,0}; d. int myList [] = {4, 3, 7}; e. int [] myList [] = [dog, cat, hamster, python];
d. int myList [] = {4, 3, 7};
21. In JAVA, the arithmetic operator "%" is applied to compute: a. integer division b. double division c. division by 0 d. the remainder of a division operation e. none of the above
d. the remainder of a division operation
Which statement is used as a catch-all category in a switch block statement?
default
What will be the output of this program? boolean b1 = true; boolean b2 = false; boolean b3 = true; if ( b1 & b2 | b2 & b3 | b2 ) System.out.print("ok "); if ( b1 & b2 | b2 & b3 | b2 | b1 ) System.out.println("dokey")
dokey
5. The numeric types and JAVA's libraries give us the ability to use JAVA as an extensive mathematical calculator. We write arithmetic expression using the built in operators along with JAVA methods from the main library. Which of the following are JAVA's built in operators a. + b. - c. * d. / e. All of them are
e. All of them are
What will be the output of this program? Test2Ans17Output p = new Test2Ans17Output(); p.start(); } void start() { boolean b1 = false; boolean b2 = fix(b1); System.out.println(b1 + " " + b2); } boolean fix(boolean b1) { b1 = true; return b1; }
false true
What will be the output of the program? int i = 1, j = 10 do { if( i > j ) { break; } j - - ; } while (++ i < 5); System.out.println("i == " + i + " and j == " + j);
i = 5 and j = 6
What is the output of the following program? char array_variable [] = new char [10]; for (int i = 0; i < 10; ++1} { array_variable ------- System.out.println(array_variable[i] + ---);
i i i i i i i i i
What causes a compiler error?
int [] [] scores = {2, 7, 6}, {9, 3,
Which one the following will declare an array and initialize it with fiver numbers not all "0"?
int [] a = {23,22,21,20,19}
What variable can be used to check the upper boundary of an array?
length
Which of these function is used to allocate memory to array variable in Java?
new
What must be used to separate each section of a for statement?
semicolons