Java for Android Week 3 Coursera
Course link : https://www.coursera.org/learn/java-for-android
Module 6 quiz on Object Oriented Programing concepts
1. An object is:
The grouping together of data and behavior to create a single entity
The blue print for creating classes
A way to hide implementation details from the user
A sequence of characters
2. A typicial object oriented program
uses methods and primitive data types to perform most of its useful behavior.
must consist of at least four classes.
uses objects to model the behavior of the ints, chars and boolean variables used in the program.
uses objects to perform most of its useful behavior.
3. An example of abstraction would be
supplying batteries with a digital camera.
supplying a Quick Start guide with a digital camera.
supplying the technical drawings with a digital camera.
only selling digital cameras to experienced users.
4. An example of an instance of the City class would be
the plans for a city.
the city of Philadelphia.
the methods to update the city’s population and to calculate the distance to other cities.
the variables Name, Latitude, Longitude, Country and Population.
5. A class file is (select all that apply)
a file containing a single program or module.
a template or blueprint for an object.
a collection of objects.
a collection of keywords.
6. A String object is (select all that apply)
a sequence of characters.
a primitive data type.
a reference to a memory location where data is stored.
similar to primitive data types in some respects.
7. Select all of the Java statements that would compile (would not cause an error).
out.println("Hello" + " programmers!");
out.println("Hello kids" - "kids");
String str1 = "hi";
String str2 = "HI";
out.println(str1 == str2);
String str1 = "hi";
String str2 = "HI";
out.println(str1.equals(str2));
String str1 = "Good programming";
out.println(str1.concat(18.9));
8. Given the following objects
String str1 = "held";
String str2;
How could you create the string herald?
str2 = str1 + "ra";
str1 = str1.substring(0,2) + "ra";
str1 = str1 + str1.substring(2,4);
str2 = str1.substring(0,2);
str2 = str2 + "ra";
str2 = str2.concat(str1.substring(2,4));
str1.substring(0,2);
str2 + "ra";
str2.concat(str1.substring(2,4));
These are answers for Java for Android Week 3
Module 6 quiz on methods and classes
1. Making instance variables of a class private
is an application of abstraction and protects the behaviors of an object from being modified by users.
is a requirement of Java. A class won’t compile unless all fields are declared private.
is an application of abstraction and protects against object users gaining direct access to object state.
is an application of security and protects against hackers modifying and corrupting our code.
2. A class method such as addMonthlyBonus(double x) that receives an input parameter and uses it to change the instance variable of an object is called a
constructor
default constructor
accessor method
mutator method
3. Select all of the following method definitions below that employ the correct syntax to define a constructor for the class Vehicle. A partial list of the class instance variables includes:
int year;
double odometer;
...
public Vehicle(){
}
public void Vehicle(double mileage){
odometer = mileage;
}
public makeVehicle(int year){
this.year = year;
}
public Vehicle(double mileage, int year){
this(mileage);
this.year = year;
}
public Vehicle(double odometer, int year){
odometer = odometer;
year = year;
}
4. When a constructor is written for the class (select all that apply)
the default constructor can not be recreated.
the default constructor is no longer available.
the default constructor must be recreated if it is to be used by client programs.
no other constructors can be written.
5. When calling one constructor from another constructor
two objects are created, one being created by each constructor.
the keyword this is used and must be the last line of the constructor code.
the keyword this is used and must be the first line of the constructor code.
the class name is used to refer to the called constructor.
6. Consider these two methods which are part of the Student class. Assume that age is a private instance variable of the class.
public void setAge(int years, int months){
age = years * 12 + months;
}
public void setAge(int months){
age = months;
}
These methods are an example of (select all that apply)
mutators.
overloading.
overriding.
accessors.
These are answers for Java for Android Week 3
Module 6 quiz on arrays and parameters
1. Which code segment illustrates a correct way to declare an array of 10 objects. Assume the class Beverage has been created.
Beverage[10] guestList;
Beverage guestlist = array[10];
new guestlist[10];
Beverage[] guestList = new Beverage[10];
2. Consider the following incorrect code segment. Assume the Pet class has been defined and contains the method setName(String).
Pet customer[] = new Pet[5];
customer[3].setName("Spot");
We learned that this code is incorrect because…select the best explanation
all five of the names must be set at one time.
although the array of Pet references has been created, none of them point to Pet objects yet. Each object must be instantiated individually.
objects stored in an array can not be accessed individually. They can only be accessed as a group.
The proper syntax for a set method called on an object is [setName(customer[3], “Spot”);]
3. Select all of the code segments that properly create an array of 3 objects and instantiate them. Assume the class Lesson has been created.
Lesson [] week1 = new Lesson[3];
for (int i = 0; i<week1.length; i++){
week1[i] = new Lesson();
}
Lesson [] week1 = {new Lesson(), new Lesson(), new Lesson()};
Lesson [] week1 = new Lesson[0], new Lesson[1], new Lesson [2];
Lesson[3] = week1[0], week1[1], week1[2];
4.
When an object is passed as a parameter, its state is changed within the method that it was passed to because…
the reference to the object was passed.
a copy of the object was passed.
the value of the object (its state) was passed.
the object was updated when the return statement was executed.
5. Consider the code
Pet sparky = new Pet();
Pet fido = sparky;
Line 2 of this code…
creates a new object named sparky with the same state data found in fido.
creates a new object named fido with the same state data found in sparky.
sets the object fido to reference or “point to” the object sparky.
compares the object fido do the object sparky and returns true or false.
6. Select all of the scenarios where using a static class constant would be appropriate.
To declare a constant that is used in different methods of the class and whose value never changes.
To declare a constant that is somehow associated with the class and may be accessed by client programs.
To declare a variable that is used in different methods of the class. This helps to avoid having to pass and return it as a parameter.
To share the state of that variable across many different objects of the class, like a count variable to keep track of how many objects have been created.
These are answers for Java for Android Week 3
Module 7 quiz on inheritance
1. Consider a class Student whose state includes name and age, Joanne who is a Student, and PartTime whose state includes all of the fields of Student and also maximumHours.
- Student is a _____________________
- Joanne is a _________________________
- PartTime is a ____________________
- sub class
- Student object
- super class
- super class
- client
- subclass
- super class
- Student object
- inherited class
- super class
- Student object
- subclass
2. When creating a subclass
only write the methods that are public.
write both the inherited and the additional state and behavior of the subclass.
only write the instance variables and methods that are added to the subclass.
only write the instance variables and methods that are inherited by the subclass.
3. Why do we aim to minimize the amount of code we have to write by not repeating code segments within a Java project?
It facilitates code reuse. An object that contains all of its state and behavior in a single file is “portable” and can be included in many different Java projects.
It makes projects unique. An object that contains all of its state and behavior in a single file is not easily reused.
It makes debugging easier. Implementation details that only appear once in a project can be corrected or improved by editing in only one place.
It makes a program run faster.
It improves readability, code is easier to trace when implementation details are found in only on location.
4. To make a call from a subclass to a public, overloaded method whose implementation details are in its super class,
simply make the call. The unique return type and/or parameter list will enable the compiler to locate the implementation details.
simply make the call. The implementation details found in the subclass will automatically be used.
you must precede the call with the keyword super to indicate that the implementation details are in the super class.
you must precede the call with the keyword sub to indicate that you are writing the method in the subclass.
5. To make a call from a subclass to a public, overridden method whose implementation details are in its super class
simply make the call. The unique return type and/or parameter list will allow the compiler to locate the implementation details.
A subclass can not call a method in its super class.
you must precede the call with the keyword super to indicate which version of the method you are calling.
you must precede the call with the keyword this to indicate which version of the method you are calling.
These are answers for Java for Android Week 3 Module 7
Module 7 quiz on code “dissection”
1. Lines 54-58 show an example of
a constructor that makes an explicit call to a constructor in the super class.
a constructor that makes an implicit call to a constructor in the super class.
a mutator method that makes an explicit call to a constructor in the super class.
a default constructor in a subclass.
2. Line _______ is the first line of a overridden method in the subclass.
Answer: 78
3. Line _______ is the first line of a mutator method that is inherited by the subclass.
Answer: 17
4. Line _________ is the first line of a method in the super class that is not inherited by the subclass.
Answer: 31
5. Select all of the lines of code that could be written in the subclass. (i.e. would not cause a compile error or a runtime error)
super.setMonth(8);
convertToString();
int num = super.getDays();
super.convertToString();
String inputString;
...
this.name = inputString;
setMonth(8);
6. Select all of the lines of code that could be written in a client file that was in the same project as the Month and SchoolMonth classes.
SchoolMonth x = new SchoolMonth(7);
x.setMonth(3);
Month x = new Month();
SchoolMonth y = new SchoolMonth(11);
y.semester = 'F';
SchoolMonth x = new SchoolMonth(7);
setMonth(4);
Month[] summer = new Month[3];
Week 2 here https://progies.in/java-for-android-week-2-coursera-quiz-and-assignments-answers