COP2800 MIDTERM (scantron 25 MC, 2-3 coding questions, plus some bonus)
What will be the result of the following string concatenation: "12" + "5"
"125"
What is the difference between the "=" operator and the "==" operator?
"=" will assign a value to a variable and "==" will check equality
What is the output of the following code? public class Array{ public static void main(String[] args) { int[] a = { 5, 0, -2, 8, 4 }; int x = a[0]; for(int i = 1; i < a.length; i++) { if(a[i] < x) { x = a[i]; } } System.out.println(x); } }
-2
After the following code finishes execution, what will the a array contain? public class Array{ public static void main(String[] args) { int[] a = { 0, 1, 0, 0, 0, 0, 0 }; for(int i = 2; i < a.length; i++) { a[i] = a[i - 1] + a[i - 2]; } } }
0 1 1 2 3 5 8
What is the output of the following code? public class Array{ public static void main(String[] args) { int[] a = { 4, 10, -5, 2, 7 }; for(int i = 0; i < a.length; i++) { System.out.print(i + " "); } } }
0 1 2 3 4
What is the output for the following code? public class Hello{ public static void main(String[] args) { /* Dear warriors, when you read this comment, * that means you've almost reached the end of * of the quiz. Congratulations!!! Trust me, there * is no trick or trap in this question. Enjoy! */ System.out.println(4/5+"," +5/5+","+1/5); } }
0,1,0
Convert the decimal number 534 to 10-bit binary (no sign bit)
1000010110
After following code snippet has be executed, ------------------------------------------- int x = 10; int y = 0; x++; do{ y = y + 10; x--; }while(x<10); ________________________________________________________________ the value of x is? the value of y is?
10; 10
convert -42 to 8-bit 2's complement
11010110
What is the result for the operation 120%240 in Java Code?
120
The && and || operators perform Conditional-AND and Conditional-OR operations on two boolean expressions. These operators exhibit "short-circuiting" behavior, which means that the second operand is evaluated only if needed. After executing following code, what will the values of x and y be? int x = 123; int y = 456; if(y==457 && x++>100){ // do something } if (x>120 || ++y>456){ // do something } System.out.println(x+","+y);
123, 456
What will be the output of the following code? int x = 10; for(int i = 0; i < 2; i++) { for(int f = i; f < 3; f++) { x++; } } System.out.println(x);
15
What will be the result of the following code: 12 + 5
17
What will be the value of x after the following code is executed? int x = 0; while(true) { if(x == 2){ break; } x++; }
2
For the following code, what is the value of k after invoking nPrint("A message", k)? public class Hello { public static void main(String[] args) { int k = 2; nPrint("A message", k); System.out.println(k); } static void nPrint(String message, int n) { while (n > 0) { System.out.println(message); n--; } } }
2 (because although n=k in the nPrint method, it goes away after you finish the nPrint method and k=2 in the main method. K and N are separate because different methods)
If you declare an array double[] list = {3.4, 2.0, 3.5, 5.5}, list[1] is ________.
2.0
What is the output for following code? public class Hello{ public static void main(String[] args) { // Surprise!!!! There is one more question. // Just as usual, there is no tricks in this question int myVariable = 5; // myVariable = 10; System.out.println(myVariable+5+10); } }
20
If you declare an array double[] list = {3.4, 2.0, 3.5, 5.5}, the highest index in array list is ______ ?
3
what is the output of the following code? public class Array{ public static void main(String[] args) { int[] a = { 4, 10, -5, 2, 7 }; for(int i = 0; i < a.length; i++) { System.out.print(a[i] + " "); } } }
4 10 -5 2 7
What is the output of the following program? public class Test { public static void main(String[] args) { int[][] matrix = {{1, 2, 3, 4}, {4, 5, 6, 7}, {8, 9, 10, 11}, {12, 13, 14, 15}}; for (int i = 0; i < 4; i++) System.out.print(matrix[1][i] + " ") } }
4 5 6 7
How many elements are in array double[] list = new double[5]
5 (inside of box parentheses = array LENGTH)
What is the output of the following program? public class Test { public static void main(String[] args) { int[][] values = {{3, 4, 5, 1}, {33, 6, 1, 2}}; for (int row = 0; row < values.length; row++) { System.out.print(m(values[row]) + " "); } } public static int m(int[] list) { int v = list[0]; for (int i = 1; i < list.length; i++) if (v < list[i]) v = list[i]; return v; } }
5 33
After the following code finishes execution, what will the b array contain? public class Array{ public static void main(String[] args) { int[] a = { 11, 17, 4, 14, 19, 6 }; int[] b = new int[a.length]; for(int i = 0; i < a.length; i++) { b[i] = a[a.length - i - 1]; } } }
6 19 14 4 17 11
What is the output for the following program? public class Hello { public static void main(String[] args) { int[] inputs = {3,4,5,6}; operation(inputs); System.out.println(inputs[2]+","+inputs[3]+","+inputs[0]+","+inputs[1]); } static void operation(int[] in) { int n = in.length - 1; while (n >= 0) { in[n]++; n--; if(n==1){ in[n]--; } } } }
6,7,4,4
one byte has ____ bits
8
What is the output of the following code? int x = 5000; if(x > 3000) { System.out.print("A"); } else if(x > 4000) { System.out.print("B"); }
A
which should be in a void method A. A method that returns a random integer from 1 to 100. B. A method that checks whether a number is from 1 to 100. C. A method that prints integers from 1 to 100. D. A method that converts an uppercase letter to lowercase.
A method that prints integers from 1 to 100
Which of the following are string concatenations that result in: "Hello, world!" "Hello" + "," + " " + "world" + "!" "Hello, wo" + "rld!" "Hello, " + "world!"
ALL
Is 9x a valid variable name?
No
Is class a valid variable name?
No
Suppose isPrime is a boolean variable, which of the following are valid ways of checking if isPrime is true? if(isPrime = true)
No
Will "D" ever be printed? if(number > 0) { System.out.println("A"); } else if(number < 0) { System.out.println("B"); } else if(number == 0) { System.out.println("C"); } else { System.out.println("D"); }
No
To output 5, which of the following statement(s) will be correct? public class Hello{ public static void main(String[] args) { // Surprise!!!! I promise this is the last surprise // Your code goes here } } System.out.println(2+3/4); System.out.println(3+5%3); System.out.println(2+3/1); System.out.println(2+3%4); System.out.println(2+3%1);
System.out.println(3+5%3); System.out.println(2+3/1); System.out.println(2+3%4);
Suppose you are working on a java source file Hello.java, the skeleton of the code looks as following: public class Hello{ public static void main(String[] args) { // Your code goes here } } To put following statements to the code, check all statements which will cause compiling errors. System.out.println(Hello, World!); System.out.println("Hello, World!") System.out.println("Hello wor"); System.Out.println("Hello, World!");
System.out.println(Hello, World!); System.out.println("Hello, World!") System.Out.println("Hello, World!");
Suppose you define a Java class as follows: public class Test { } In order to compile this program, the source code should be stored in a file named:
Test.java
(True/False) In Java, all code must be put inside of a Class.
True
Is $33 a valid variable name?
Yes
Is radius a valid variable name?
Yes
which data type should the following method return? public static _________ getGrade(double score) { if (score >= 90.0) return 'A'; else if (score >= 80.0) return 'B'; else if (score >= 70.0) return 'C'; else if (score >= 60.0) return 'D'; else return 'F'; }
char (because it's in single quotes)
What will the value of myBool be after this line executes? boolean myBool = 5 * 2 > 8 && 2 + 2 <= 3;
false
The JDK command that will run the Java application called ByteCode.class is
java ByteCode
The JDK command to compile a class in the file Test.java is
javac Test.java
arguments to methods always appear within _________.
parentheses
The first line of code of the main method is called what?
the entry point of a Java program
What will be the value of x after this code finishes execution? int x = 5; for(int i = 0; i < 5; i += 0) { x += 5; }
the loop condition is always true and will execute forever (because you're not adding anything to i)
Suppose isPrime is a boolean variable, which of the following are valid ways of checking if isPrime is true? if(isPrime != false)
yes
Suppose isPrime is a boolean variable, which of the following are valid ways of checking if isPrime is true? if(isPrime == true)
yes
Suppose isPrime is a boolean variable, which of the following are valid ways of checking if isPrime is true? if(isPrime)
yes