This segment of code is found in every program we have made up to this point - though
normally it is part of the starter code. Think back to the code practices, choose the
correct key words in the correct order that should be used to fill in the blanks:
______ ______ ______ main (String[] args)
{
}
public, static, void
A special value that means "no object" is called:
null
Consider the following code:
String w = "potato";
if (w.indexOf('e') == -1)
{
w = w + "es";
}
else
{
w = w + "s";
}
System.out.println(w);
What is output?
potatoes
A ______ is a separate chunk of code that is given a name.
method
When you need to use a method, you ______ it.
call
What symbols help to section off the code that is part of a given method?
{}
Which of the following is a correct method header?
_____ _____ _____ doStuff()
public static void
Consider the following code:
public static void doStuff(){int x = 82;if (x % 10 >= 5){System.out.println("first");}else if (x
% 5 >= 2){System.out.println("second");}else if (x % 20 >= 10)
{System.out.println("third");}else if (x % 6 == 2){System.out.println("fourth");}}
What is displayed by the call doStuff()?
second
A(n) ______ is the local variable in a method that holds the data sent in.
formal parameter
A(n) ______ is the data sent to a method.
actual parameter
Which of the following are NOT a legal call to the method:
public static void powerOfTwo(double x){System.out.println(Math.pow(2, x));}
All of the items listed are legal method calls.
, Which of the following uses a parameter correctly?
public static void printBox(int s){//Code}
Consider the following method header:
public static void doThings(int a, double b) {//Code}
Which of the following is a correct call to this method?
doThings(7, 2.5);
Consider the following java program.
class Example {public static void myMethod() {System.out.print("hello ");}public static
void main(String[] args) {myMethod();myMethod();}}
What is printed when this program runs?
hello hello
What is wrong with the following code which uses the circle class from the
edhesive.shapes.Circle class?
Circle c = new Circle();double r = c.setRadius(6.5);
The setRadius method is void, so it doesn't have a return to be stored in a value.
Consider the following code:
class Example {public static void main(String[] args) {doStuff();doStuff();public static
void doStuff() {System.out.println("You called?");}}}
Why does this cause an error when compiled?
The method doStuff can't be defined inside of the main method.
What key word shows that a method will not return a value?
void
Consider the method defined below:
public static void answerPhone() {System.out.println("Ahoy-hoy!");}
How would you call this method from main?
answerPhone();
Looking at the following method:
public static void repeatPrint(String s, int n) {for (int i = 0; i < n; i++) {System.out.print(s +
" ");}}
Which of the parameters are class-type, and which are primitive?
s is class-type, n is primitive
Again, looking at the same method as above:
public static void repeatPrint(String s, int n) {for (int i = 0; i < n; i++) {System.out.print(s +
" ");}}
What is printed when the following call is made from main?
repeatPrint("done", 3);
done done done
When you pass an object to a method, the method receives ______.
a copy of the reference to the object
Consider the following code, which appears in the main method of a class
RegularPolygon myShape = new RegularPolygon(5,
3.5);doStuff(myShape);System.out.println(myShape);
Where doStuff is defined in the same class as:
public static void doStuff(RegularPolygon r) {r.addSides(3);r.setSideLength(2.0);}
What will be printed by the code in main?
regular octagon with side length 2.0