Unit 5 Edhesive Review

¡Supera tus tareas y exámenes ahora con Quizwiz!

Consider the following method: public static void doStuff(int a, int b) {a++;b++;System.out.println(a + " " + b);} What is output by the following code? int x = 7;int y = 5;doStuff(x, y);System.out.println(x + " " + y);

8 67 5

Consider the following method: public static Circle doubleRad(Circle c) {c.setRadius(2 * c.getRadius());return c;} What will be printed when the following code in the main method of the program is run? Circle circ1 = new Circle(3.0);Circle circ2 = doubleRad(circ1);System.out.println(circ1);System.out.println(circ2);

circle with radius 6.0circle with radius 6.0

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

A ______ is a separate chunk of code that is given a name.

method

Classes include variables and ______.

methods

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

All instance variables should be declared as _______

private

Consider the following method: public static int doubleVal(int n) {n *= 2;return n;} What will be printed when the following code in the main method of the program is run? int a = 5;int b = doubleVal(a);System.out.print(a + " " + b);

5 10

Consider the following code: int num = 80;doStuff(num); Where doStuff is defined as: public static void doStuff(int b) {b--;} What value is stored in num after the doStuff() method is called and the code inside of doStuff() is run?

80

Given the following method definition: public static void doStuff(int a){ a++;} What is output by the following? int x = 9;doStuff(x);System.out.println(x);

9

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.

The year variable for a Book is set to 2001. Suppose the variable b in a separate class points to this book. Which of the following calls would return the year value of 2001?

b.getYear()

When this type of variable is passed into a method, any changes made to it in the method are saved and the previous value of the variable is overwritten outside the method.

class

A(n) ______ is the local variable in a method that holds the data sent in.

formal parameter

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

Suppose a method p has the following heading: public static Circle p() Which return statement may be used in p()?

return new Circle(6.5);

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

Consider the following code: int b = -3;int e = 2;int x = Math.abs((int)Math.pow(b, e));if (x == 4) {System.out.print("first");}else if (x == 9) {System.out.print("second");}else if (x == -9) {System.out.print("third");}else if (x == -9) {System.out.print("fourth");}else{System.out.print("fifth");} What will be printed?

second

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

What symbols help to section off the code that is part of a given method?

{ }

A(n) ______ method does not return a value.

void

What key word shows that a method will not return a value?

void

Which of the following is true about methods?

A method in java can only return one value.

When you pass an object to a method, the method receives ______.

a copy of the reference to the object

A(n) ______ is the data sent to a method.

actual parameter

When a method needs to send a value back to a calling method, it uses the ______ command.

return

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.

Consider the following method: public static int doStuff(double val) {return (int) (10 * val) / 10;} Which of the following calls to this method is not legal?

String b = doStuff(3.5);

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.

The following code appears in the main method of another class. Book b = new Book("Emily Bronte", "Wuthering Heights", 1847);System.out.println(b); Which of the following best describes what appears on the screen when this code is executed?

The values "Wuthering Heights", "Emily Bronte" and "1847" with each on a new line, and the second two lines indented.

When a parameter which is of a mutable class type (i.e. not a String) is passed to a method in Java, ______.

all changes are saved because a copy of the reference to the memory address is passed

Consider the method defined below: public static void answerPhone() {System.out.println("Ahoy-hoy!");} How would you call this method from main?

answerPhone();

When you need to use a method, you ______ it.

call

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);

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

What is printed when the following code, which appears in the main method of a separate class to Book, is run? Book b1 = new Book("George Orwell", "Nineteen Eighty-Four", 1949);Book b2 = new Book("George Orwell", "Nineteen Eighty-Four", 1949);if (b1 == b2){System.out.print("equal, ");}else{System.out.print("not equal, ");}if (b1.equals(b2)){System.out.print("equal");}else{System.out.print("not equal");}

not equal, equal

The following four questions refer to the Book class defined below: public class Book { private String title; private String author; private int year; /* constructor header not shown */ { author = a; title = t;setYear(y); }public String toString() { return title + "\n\t" + author + "\n\t" + year; } public int getYear() { return year; }public void setYear(int y) { if (y >= 1450) { year = y; } else { year = 1900; } }public boolean equals(Book other) {if (this.title.equals(other.title) && this.author.equals(other.author)&& this.year == other.year){return true;}return false;}} The header for the constructor of this class is missing. Which of the following would be the best header to use for this constructor?

public Book(String a, String t, int y)

Which of the following is the correct header for a method which takes one int parameter and returns a Circle?

public static Circle myMethod(int n)

Which of the following is a correct method header? _____ _____ _____ doStuff()

public static void

Which of the following uses a parameter correctly?

public static void printBox(int s){//Code}

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

Consider the following method declaration. public static void reset(Rectangle r, Circle c) {r = new Rectangle(1.0, 1.0);c.setRadius(1.0);} If the following code in the main method of the program is run, what will be printed? Rectangle rect = new Rectangle(5, 7);Circle circ = new Circle(9.0);reset(rect, circ);System.out.println(rect);System.out.println(circ);

rectangle with length 5.0, width 7.0 circle with radius 1.0


Conjuntos de estudio relacionados

Chapter 8 Quiz :) (study guide), Chapter 9 Study

View Set

Ch 2 smart book practice quiz BA 370 SDSU

View Set

Chp 8 Intrapartum assessment & intervention :)

View Set

10 Most Commom Financial Mistakes

View Set

Indiana Life & Health Insurance Exam Review

View Set

Housing & Interior Unit 2 Test Review:

View Set