CSE 174 (Week 7)
three
Given the value of i = 2, what will be printed on executing the following code? switch(i) { case 1: System.out.println("one"); break; case 2: case 3: System.out.println("three"); break; default: System.out.println("zero"); }
This is default This is case 1
Given, int i=4, what is printed as output when the following switch statement is executed? switch(i) { default: System.out.print("This is default"); case 1: System.out.print("This is case 1"); break; case 2: System.out.print("This is case 2"); break; case 3: System.out.print("This is case 3"); }
a=a>b?c>d?1:2:3;
Identify the equivalent code for the following nested if statement. if (a>b) if (c>d) a=1; else a= 2; else a = 3; - None of the above - a=a>b?1:c>d:2:3; - a=a>b?c>d?1:2:3; - a=a>b?1:c>d?2:3;
0 and 1
In the previous question, for what value(s) of i case 1 is executed? int i=_____; switch(i) { default: System.out.println("zero"); case 1: System.out.println("one"); break; case 2: System.out.println("two"); break; case 3: System.out.println("three"); }
false
Suppose the int variable n has been declared and assigned a value. What is the value of the following boolean expression? (n > 10 && n < 10) - true - it could be true or false, depending on the value in n - false - error
-it could be true or false, depending on the value in n
Suppose the int variable n has been declared and assigned a value. What is the value of the following boolean expression? (n > 10 || n < 10) - it could be true or false, depending on the value in n - true - false - error
False
True or False: The following code snippet has no compilation errors. public static void main(String[] args) { { int x = 2; System.out.println(x); } System.out.println(x); }
False
True or False: The following code snippet never prints the message Good for any boolean expression that can be filled in the blank. double x = 2.04; if ((___%2)==0) System.out.print("Good ");
False
True or False: The statement switch(i) { } with an empty body results in a compiler error.
True
True or False: The variables name1 and name2 refer to the same string literal "Bob". String name1="Bob"; String name2="Bo"+"b";
- double answer = avg(2.0, 3.0, 4.0, 5.0);
Consider a method named avg, which takes four double values and returns their average as an int. Which one of the following is a correct call to the method avg? - int answer = avg(2, 3, 3, 5,1); - double answer = avg(2.0, 3.0, 4.0, 5.0); - int answer = avg(3.5, 4.5, 6.5); - double answer = avg(3, 4, 6);
Error: void type not allowed here.
Consider the following definition of add() method. public static void add(int a,int b) { System.out.println(a+b); } What would be printed if the following statement is executed from main()? System.out.println(add(1,2));
Error: Missing return statement
Consider the following modified definition of add() method. public static int add(int a,int b) { System.out.println(a+b); } What would be printed if the following statement is executed from main()? System.out.println(add(1,2));
false
Consider two variables a and b of type double that hold the values 0.001 and 1.001 respectively. What does the following call to the chkDouble() method print? System.out.println(chkDouble(a,b)); public static boolean chkDouble(double a, double b) { if (b-a == 1.0) return true; else return false; }
7 a= 4 b= 3
What does the following method inc() print when it is called from main() as inc(a,b); //a = 2 and b = 3 public static void inc(int b,int a) { System.out.print(++b + ++a); System.out.print(" a= " + a + " b= " + b); }
-missing return statement
What is the compiler error in the following method definition? public static String result(int weight){ if (weight <= 30) return "cat"; if (weight >= 10) return "dog";} - multiple return statements - missing return statement - invalid return type - missing else statement
- The method does not contain a compiler error.
What is the compiler error, if any, in the following method definition? public static String circumference(double r){ double result; result = 2 * 3.14 * 87; return "cat";} - The method does not make use of the parameter variable r. - The method does not return the value result. - The variable result is set but never used. - The method does not contain a compiler error.
- The method returns a value of type double.
What is the compiler error, if any, in the following method definition? public static int tripler(int numPara){ double result = numPara * 3; return result; } - The method does not modify its parameter variable - The method does not contain a compiler error - The method does not return a value. - The method returns a value of type double.
-16
What is the last value printed by the following code fragment? int i = 4; while (i < 14) { i = i + 3; System.out.println(i); } - 14 - 16 - 4 - 13
-true
What is the output of the code snippet below? int a = 0; System.out.println(a >= 0 || 10 / a > 2); - true - error because of dividing 10 by zero - false - error because you cannot print a boolean expression
truetrue
What is the output? int a=100, b=200; System.out.print(a == 100 || ++b > 200); if (b==200) System.out.println(true); else System.out.println(false);
11 10
What is the output? public static void main(String[] args) { int i = 10; c(i); System.out.print(i); } public static void c(int i) { System.out.println(++i); }
1 2
What is the output? switch((int)1.5) { default : System.out.println("0"); case 1: System.out.println("1"); case 2: System.out.println("2"); }
2
What value is assigned to the variable a in the following statement? a=10,b=2,c=3,d=4,e=5; a=a>b?c>d?1:e>d?2:3:4;
30
What value is printed if you execute the following code? int k, num=30; k = (num>5 ? (num <=10 ? 100 : 200): 500); System.out.println(num); - 500 - 100 - 30 - 200
Compiler Error in Line 1
What will be printed if both the variables me and you in the following code are initialized with the value 1.1 in Line 1.? 1: float you, me; 2: if(me==you) 3: System.out.printf("C "); 4: else 5: System.out.printf("Java"); - Compiler Error in Line 1 - Compiler Error in Line 3 - Java - C
Compiler Error
What will be printed if the following Java snippet is executed? int i=1,j=2; switch(i) { case 1: System.out.println("GOOD"); break; case j: System.out.println("BAD"); break; } System.out.println("End"); - BADEnd - GOODEnd - Compiler Error - GoodEnd
three
What will be printed on executing the following switch statement? int i=3; switch(i) { default: System.out.println("zero"); case 1: System.out.println("one"); break; case 2: System.out.println("two"); break; case 3: System.out.println("three"); }
Less than 5
What will be the value of num if the following code prints 500 for the following program? int k, num=__; k = num>5 ? num <=10 ? 100 : 200: 500; System.out.println(k); - Between 5 and 10 - Less than 5 - Greater than 10 - None of the above
3
What will be the value of num if the following code prints 500 for the following program? int k, num=__; k = num>5 ? num <=10 ? 100 : 200: 500; System.out.println(k); - 13 - 3 - 10 - 8
-Math.abs(a - b) < .0001
Which boolean expression below is best for checking to see if the double values a and b are very close together? - Math.abs(a - b) < .0001 - a == b - a - b > .0001 && a - b < .0001 - a.equals(b)
There is no syntax error in the if statement
Which of the comment(s) is true about the following Java code? if (7<3); System.out.print("Good or bad"); - Placing a semicolon immediately after the right parenthesis of the condition in an if structure is a logic error. - Placing a semicolon immediately after the right parenthesis of the condition in an if structure is a syntax error. - None of the above - There is no syntax error in the if statement
- (time<20)
Which of the following boolean expressions is equivalent to: ! - (time<20) - (time >= 20) - (time <= 20) - (time !<20) - (time > 20)
- Put as many statements as possible into the main method
Which of the following is NOT a good practice when developing a computer program? - Place code that is used multiple times into a separate method - Put as many statements as possible into the main method - Decompose a program into many small methods - Document the purpose of each method parameter
int y = 20 - 30.0; int b=10.0; float you = 1.1, me = 1.1; float y = 20 - 30.0;
Which of the following statements produce a compile time error? float you = 1.1, me = 1.1; int a=10,b; int b=10.0; double x = -20; boolean x=(10<20); int y = 20 - 30.0; float y = 20 - 30.0; double y = 20 - 30.0; float z = 2 - 10.0f;
- Methods can have multiple parameters and can return one return val
Which one of the following is true about methods? - The word keyword return can only appear one time in a method - Methods can have multiple parameters and can return multiple return values - Methods can have one parameter and can return multiple return values - Methods can have multiple parameters and can return one return value - Methods can have only one parameter and can return only one return value
- Using a String to store the value returned by the charAt() method of the String class
Which one of the following would be an incorrect choice for a data type: - Using a String to store the value returned by the charAt() method of the String class - Using an int to store the value returned by the indexOf() method of the String class - Using a double to store the value returned by the sqrt() method of the Math class - Using a String to store the value returned by the next() method of the Scanner class