CH. 6 QUIZ QUESTIONS

5 September 2022
4.7 (114 reviews)
26 test answers

Unlock all answers in this set

Unlock answers (22)
question
A method can be defined inside a method in Java True or false?
answer
False
question
A method can be declared with no parameters True or false?
answer
True
question
A ___________ method does not return a value. -void -non-void
answer
void
question
(int)(Math.random() * (65535 + 1)) returns a random number ___________. A- between 1 and 65536 B- between 1 and 65535 C- between 0 and 65535 D- between 0 and 65536
answer
C - between 0 and 65535
question
A return statement without any value can be used in _________. - a non-void method - void method
answer
void method
question
A variable defined inside a method is referred to as ________. - a global variable - a method variable - a block variable - a local variable
answer
local variable
question
A variable or a value listed in a call to a method is called ________. - a parameter - an argument - a local variable - a global variable
answer
an argument
question
A variable that is declared inside a method is called ________. - a static - an instance - a local - a global - a class
answer
a local
question
All Java applications must have a method ________.
answer
public static void main(String[] args)
question
Analyze the following code: public class Test { public static void main(String[] args) { System.out.println(xMethod(5, 500L)); } public static int xMethod(int n, long l) { System.out.println("int, long"); return n; } public static long xMethod(long n, long l) { System.out.println("long, long"); return n; } } (A)-The program displays int, long followed by 5 (B)-The program displays long, long followed by 5 (C)-The program runs fine but displays things other than 5 (D)-The program doesnt compile bc the compiler cant distinguish which xmethod to invoke
answer
A- The program displays int, long followed by 5
question
Analyze the following code: public class Test { public static void main(String[] args) { System.out.println(max(1,2)); } public static double max(int num1, double num2) { System.out.println("max(int, double) is invoked"); if (num1 > num2) return num1; else return num2 } public static double max(double num1, int num2) { System.out.println("max(double, int) is invoked"); if (num1 > num2) return num1; else return num2; } }
answer
Program can't choose which max method to invoke
question
Arguments to methods always appear within ____. -brackets -parantheses -curly braces -quotation marks
answer
Parantheses
question
Given the following method: static void nPrint(String message, int n) { while (n > 0) { System.out.print(message); n--; } } What is k after invoking nPrint("A message", k)? int k = 2; nPrint("A message", k);
answer
2 k is not n, k is outside while loop
question
______ is to implement one method in the structure chart at a time from the top to the bottom. - Bottom-up approach - Top-down approach - Bottom-up and top-down approach - Stepwise refinement
answer
Top-down approach
question
_______ is a simply but incomplete version of a method - A stub - A main method - A non-main method - A method developed using top-down approach
answer
A stub
question
You should fill in the blank in the following code with ________. public class Test { public static void main(String[] args) { System.out.print("The grade is " + getGrade(78.5)); System.out.print("nThe grade is " + getGrade(59.5)); } public static ________ getGrade(double score) { if (score >= 90.0); System.out.println( 'A'); else if (score >= 80.0); System.out.println('B'); else if (score >= 70.0); System.out.println( 'C'); else System.out.println( 'F'); } }
answer
void
question
You should fill in the blank in the following code with ________. public class Test { public static void main(String[] args) { System.out.print("The grade is " + getGrade(78.5)); System.out.print("nThe grade is " + getGrade(59.5)); } 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 return 'F'; } }
answer
char (non-void)
question
You must have a return statement in a non-void method. True or false?
answer
True
question
You can redeclare the variable if it is declared in the method signature. True or false?
answer
False
question
You can define two methods in the same class with the same name and parameter list. True or false?
answer
False
question
Which of the following is not an advantage of using methods? -Using methods makes programs run faster. -Using methods makes reusing code easier. -Using methods makes programs easier to read. -Using methods hides detailed implementation from the clients.
answer
-Using methods makes programs run faster
question
The signature of a method consists of _________. -method name -method name and parameter list -return type, method name, and parameter list -parameter list
answer
-method name and parameter list
question
The actual parameters of a method must match the formal parameters in type, order, and number. True or false?
answer
True
question
Methods can be declared in any order in a class. True or false?
answer
True
question
Java allows you to declare methods with the same name in a class. This is called _________. -method duplication -method overriding -method overloading -method redeclaration
answer
-method overloading
question
Does the return statement in the following method cause compiler errors? public static void main(String[] args) { int max = 0; if (max != 0); System.out.println(max); else return; } Yes or no?
answer
No