Java Quiz 6 - Functions

25 July 2022
4.7 (114 reviews)
18 test answers

Unlock all answers in this set

Unlock answers (14)
question
1. Suppose your method does not return any value, which of the following keywords can be used as a return type? A) public B) double C) int D) void E) None of the above
answer
ANS: D
question
2. When a method tests an argument and returns a true or false value, it should return a. a zero for true and a one for false b. a boolean value c. a zero for false and a non-zero for true d. a method should not be used for this type test
answer
ANS: B
question
3. What will be returned from the following method? public static double methodA() { double a = 8.5 + 9.5; return a; } a. 18.0 b. 18 (an integer) c. 8 d this is an error
answer
ANS: A
question
4. What is wrong with the following method call? displayValue (double x); a. There is nothing wrong with the statement. b. displayValue will not accept a parameter. c. Do not include the data type in the method call. d. x should be a String.
answer
ANS: C
question
5. Which of the following would be a valid method call for the following method? public static void showProduct (int num1, double num2) { int product; product = num1 * (int)num2; System.out.println("The product is " + product); } a. showProduct(5.5, 4.0); b. showProduct(10.0, 4); c. showProduct(10, 4.5); d. showProduct(33.0, 55.0);
answer
ANS: C
question
6. Given the following method static void nPrint(String message, int n) { while (n > 0) { System.out.print(message); n--; } } What is the printout of the call nPrint('a', 4)? A) invalid call B) aaaa C) aaa D) aaaaa
answer
ANS: A because the function expects a string as first parameter and the call is passing a char
question
7. Given the following method header, which of the method calls would be an error? public void displayValues(int x, int y) a. displayValue(a,b); // where a is a short and b is a byte b. displayValue(a,b); // where a is an int and b is a byte c. displayValue(a,b); // where a is a short and b is a long d. they would all give an error
answer
ANS: C
question
8. Does the return statement in the following method cause compile errors? public static void main(String[ ] args) { int max = 0; if (max != 0) System.out.println(max); else return; } A) Yes B) No
answer
ANS: B
question
9. If method A calls method B, and method B calls method C, and method C calls method D, when method D finishes, what happens? a. control is returned to method A b. control is returned to method B c. control is returned to method C d. the program terminates
answer
ANS: C
question
10. What will be the output of the following code? public static void main(String[] args) { System.out.print("Enter B "); method1(); System.out.print("Leave B "); } public static void method1() { System.out.print("Enter 1 "); method2(); System.out.print("Leave 1 "); } public static void method2() {; System.out.print("Enter 2 "); System.out.print("*** "); System.out.print("Leave 2 "); }
answer
Ans: Enter B Enter 1 Enter 2 *** Leave 2 Leave 1 Leave B
question
11. What will be the output of the following code? public static void main(String[] args) { System.out.print("Today "); whatDay(); System.out.print(" of the rest of your life"); } public static void whatDay() { System.out.print("is the first day"); }
answer
ANS. Today is the first day of the rest of your life
question
12. What will be the output of the following code? public static void main(String args[]) { int num = 10; displayMult(num); num = 5; displayMult(num); num = 2; displayMult(num); } public static void displayMult(int num) { if( num <= 3 ) { System.out.print(3 * num); } else { if( num > 7) { System.out.print (7 * num); } } } a. 7014 b. 30614 c. 706 d. no output
answer
Ans. C
question
13. What will be the output of the following code? public static void main(String[] args) { System.out.print( func1(10)); } public static String func1(int y) { if( y > 2) return ">2"; if( y > 3 ) return ">3"; return "<2"; }
answer
Ans. >2
question
14. A variable defined inside a method is referred to as ________. A) a global variable B) a local variable C) a block variable D) a method variable
answer
ANS: B
question
15. What is k after the following block executes? { int k = 2; nPrint("A message", k); } System.out.println(k); A) 2 B) 1 C) 0 D) k is not defined outside the block. So, the program has a compile error
answer
ANS: D
question
16. What will be the output of the following code? public static void main(String[] args) { String a, b; a = "A"; b = "B"; printWords(a, b); printWords(b, a); } public static void printWords(String a, String b) { System.out.print(a + b); } a. ab ba b. abba c. ABBA d. AB BA
answer
ANS: C
question
17. What will be the output of the following code? public static void main(String args[]) { String word1, word2, word3; word1 = "First"; word2 = "Second"; word3 = "Third"; myproc(word1, word2, word3); } public static void myproc(String var3, String var2, String var1) { System.out.print (var1 + var2 + var3); } a. FirstSecondThird b. ThirdSecondFirst c. SecondThirdFirst d. No output
answer
ANS: B
question
18. What will be the output of the following code? public static void main(String[] args) { method3(0); method3(1); } public static void method3(int i){ int[] sum = new int[2]; sum[i] = i + 5; System.out.print(sum[0] + "," + sum[1] + ","); }
answer
Ans. 5,0,0,6,