09.winter.quiz1.sol
DESCRIPTION
javaTRANSCRIPT
1
ISOM232 Internet Applications Development
Quiz #1
1 What is the printout of the following Java code: ... double x = 10.1; int y = (int)x; System.out.println("x is " + x + " and y is " + y); ...
A x is 10 and y is 10 B x is 10.0 and y is 10.0 C x is 11 and y is 11 D x is 10.1 and y is 10
2 Analyze the following code:
... // Enter an integer String numString = JOptionPane.showInputDialog(null, "Enter a number:", "Exam Input", JOptionPane.QUESTION_MESSAGE); int number = Integer.parseInt(numString); if (number <= 0) System.out.println(number); ...
A The if statement is wrong, because it does not have the else clause B System.out.println(number); must be placed inside braces { } C If number is zero, number is displayed D If number is positive, number is displayed
2
3 What is the output of the following code: ... int x = 9; int y = 8; int z = 7; if (x > 9) if (y > 8) System.out.println("x > 9 and y > 8"); else if (z >= 7) System.out.println("x <= 9 and z >= 7"); else System.out.println("x <= 9 and z < 7"); ...
A x > 9 and y > 8; B x <= 9 and z >= 7; C x <= 9 and z < 7; D None of the above.
4 What will be y after executing the following statements?
... int x = 0; int y = 0; switch (x + 1) { case 0: y = 0; case 1: y = 1; default: y = -1; } ...
A 1 B ‐1 C 0 D None of the above
3
5 What will be the value of balance after executing the following code? ... int balance = 10; while (balance >= 1) { if (balance < 9) continue; balance = balance - 9; } ...
A ‐1 B 0 C 1 D The loop will not end
6 What will be the value of balance after executing the following code?
... int balance = 10; while (balance >= 1) { if (balance < 9) break; balance = balance - 9; } ...
A –1 B 0 C 1 D None of the above
7 What is the output of the following program?
public class TestMath { public static void main(String args[]){ for (int i = 0, j = 0; (i + j < 5); i++, j++) { System.out.print(i+j); } } }
A 123 B 135 C 012 D 024
4
8 What is the output of the following program? public class TestOverloading { public static void main(String[] args) { int a = 1; int b = 2; double sum_double; sum_double = total(a,b); System.out.println(sum_double); } public static double total(int num1, int num2) { double output = num1 + num2; return output; } public static int total(long num1, int num2) { return num1 + num2; } }
A 3 B 3.0 C The program cannot be compiled. D None of the above
9 What are the outputs of the following program?
public class TestPrePost { public static void main(String[] args){ int i = 1; int k = ++i + i; System.out.println("i = " + i); System.out.println("k = " + k); } }
A i = 2 k = 3 B i = 2 k = 4 C i = 3 k = 3 D i = 3 k = 4
5
10 Analyze the following code: class Test { public static void main(String[] args) { System.out.println(xMethod((double)5)); } public static int xMethod(int n) { System.out.println("int"); return n; } public static long xMethod(long n) { System.out.println("long"); return n; } }
A The program displays int followed by 5. B The program displays long followed by 5. C The program runs fine but displays things other than given in a and b. D The program cannot be compiled.
11 Analyze the following code:
... public static void main(String[] args) { for (int i = 1; i < 10; i++) { int k = 3; System.out.println("A message", k); } System.out.println("k is " + k); } ...
A The code has a syntax error because k is not defined in System.out.println("k is " + k). B The code prints k is 0. C The code prints k is 1. D The code prints k is 2.
6
12 Analyze the following code: public class Test { public static void main(String[] args) { int n = 2; xMethod(n); System.out.println("n is " + n); } public static void xMethod(int n) { n++; } }
A The code prints n is 1. B The code prints n is 2. C The code prints n is 3. D The code has a syntax error because xMethod does not return a value.
13 Analyze the following program:
public class WelcomeInMessageDialogBox { public static void main(String[] args) { // Display Welcome to Java! in a message dialog box JOptionPane.showMessageDialog(null, "Welcome to Java!"); } }
A A message dialog box with “Welcome to Java” is displayed. B The program cannot be compiled unless “import javax.swing.*;” is inserted in the first
line. C The usage of JOptionPane.showMessageDialog is incorrect. D None of the above
7
14 What is the output of the following program: ... int a = 5; int b = 6; System.out.println(((a>b) == true)? a : b); ...
A 5 B 6 C The program cannot be compiled. D None of the above
15 Analyze the following code:
... 01 public static int OK(long n) { 02 if (n > 0) 03 return 1; 04 else if (!true) 05 return 0; 06 else if (n = 0) 07 return n; 08 } ...
Where are the possible errors? A Line 04 B Line 06 C Line 07 D Line 06 and 07