Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1) Suppose we’ve defined a book class to include a setTitle method with a protot

ID: 3770314 • Letter: 1

Question

1) Suppose we’ve defined a book class to include a setTitle method with a prototype:

void setTitle(char *);

Suppose further we have an instance of the book class named bookOne. Determine which of the following statements correctly invokes the setTitle method:

a) title = bookOne.setTitle(“Book One”);

b) title = book.setTitle(“Book One”);

c) bookOne.setTitle(“Book One”);

d) book.setTitle(“Book One”);

Answer: ________________.

2) Let v be a static variable defined in a class called C

a) v gets initialized every time an instance of C is created.

b) v retains same value for all instances of C

c) v can only be accessed by non-class functions

d) None of the above

Answer: ________________.

Explanation / Answer

1) answer: c)

Reason: since setTitle() method is a void function hence no value will be returned hence a) and b) options are out. Further we call a method on a variable of that class. We do not call a method on the name of that class here name of variable of class is bookOne hence we call setTitle() method using "bookOne.setTitle(“Book One”);"

2) answer b)

static variable has special property of getting itself shared among the instances of the class. hence it retains its value. If one instance of a class makes changes in a static variable of that class then the change will be reflected upon all other instances of that class.