Object Oriented Programming in Java Coursera Quiz Answers

Object Oriented Programming in Java Week 1 Quiz Answers

Quiz: Objects, memory models, and scope

Q1. Consider the following code:

public class MyClass
{  
private int a;  
public double b;    
public MyClass(int first, double second)  
{    
this.a = first;    
this.b = second;  
}  
public static void main(String[] args)  
{    
MyClass c1 = new MyClass(10, 20.5);    
MyClass c2 = new MyClass(10, 31.5);    
System.out.println(c1.a + ", " + c1.b);  
}
}

What would this code print?

Note that this might seem like a compiler error to be accessing c1.a and c1.b from main, but because the method main is defined in the class “MyClass”, it will compile just fine (feel free to check it for yourself).

  • 10, 20.5
  • 10, 31.5

Q2.

 public class MyClass 
{   
private int a;  
public double b;     public MyClass(int first, double second)  
{    
this.a = first;    
this.b = second;  
}  
public static void main(String[] args)  
{    
MyClass c1 = new MyClass(10, 20.5);   
 MyClass c2 = new MyClass(10, 31.5);    
// lines below are changed from the question above   
 c2 = c1;       
c1.a = 2;    
System.out.println(c2.a);  
}
}

What is the result of running the code above? Hint – draw a memory model!

  • 10
  • 2

Q3. In the code from question 2 above, after executing c2 = c1, how can you get back the object which c2 previously pointed to (the one with a as 10 and b as 31.5)?

  • You cannot get that object back
  • c2–;
  • c2.restore()

Q4. Please review the code below:

public class MyClass
{  
private int a;  

public double b;    
public MyClass(int first, double second)  
{    
this.a = first;   
 this.b = second;  
}  
 // new method  public static void incrementBoth(MyClass c1) 
{    
c1.a = c1.a + 1;    
c1.b = c1.b + 1.0;  
}   
public static void main(String[] args) 
 {    
MyClass c1 = new MyClass(10, 20.5);   
 MyClass c2 = new MyClass(10, 31.5);   
 // different code below    incrementBoth(c2);   
 System.out.println(c1.a + ", "+ c2.a);  
}
}

What would be the output from running main?

  • 10, 11
  • 11, 11
  • 10, 10
  • 11, 10

Q5. Please review the code below:

public class MyClass
{  
private int a;  
public double b;    
public MyClass(int first, double second) 
 {   
 this.a = first;    
this.b = second;  
}    
public static void incrementBoth(MyClass c1) 
{    
c1.a = c1.a + 1;    
c1.b = c1.b + 1.0;  }   
 // new method  public static void incrementA(int first) 
 {    
first = first + 1;  
}    
// new method  public static void incrementB(double second)  
{    
second = second + 1.0;  
}    
public static void main(String[] args)  
{    
MyClass c1 = new MyClass(10, 20.5);    
MyClass c2 = new MyClass(10, 31.5);   
 // different code below    incrementA(c2.a);    
incrementB(c2.b);    
System.out.println(c2.a + ", "+ c2.b);  
}
}

What is the output from running this code?

  • 10, 31.5
  • 11, 32.5

Object Oriented Programming in Java Week 2 Quiz Answers

Quiz: Programming Assignment Quiz (Do programming assignment FIRST)

Q1. Does your map display dozens of earthquake markers, with different colors and sizes indicating the strength of each earthquake that seem to make sense?

  • Yes
  • Not yet…

Q2. Does your map display a key that shows an explanation of each type of earthquake marker, and is that key neatly organized, such as in the image below?

  • Yes
  • Not yet…

Q3. Which class do you create objects of to display the earthquake “dots” on the map?

  • earthquakes
  • SimplePointMarker
  • PointFeature
  • Marker

Q4. Assume you have a SimplePointMarker stored in the variable marker. Which of the following lines of code will set that marker’s color to red?

marker.setColor(255, 0, 0);
 marker.setColor(color(255, 0, 0));
 
 marker.setColor(255); 
  • correct Answer

Q5. How many different SimplePointMarker objects does your program create when you run it?

  • One for each PointFeature in earthquakes
  • 100
  • 1

Q6. What was the most difficult or confusing part of this assignment for you? If more than one was difficult, select what you felt was the most difficult.

  • Getting oriented with the starter code.
  • Getting the syntax right (semi-colons, braces, etc)
  • Figuring out the names of the built-in classes and methods and how to use them.
  • Getting the logic right for creating or styling markers
  • Drawing the key
  • Nothing was difficult
  • Something else not listed above

Object Oriented Programming in Java Week 3 Quiz Answers

Quiz: Programming Assignment Quiz (Do programming assignment FIRST)

Q1. Does your earthquake map display earthquakes, cities and the key, as described through the end of step 9, as shown in this screenshot?

Object Oriented Programming in Java Coursera Quiz Answers

  • Yes
  • No

Q2. When you first ran the starter code for the programming assignment, why didn’t it display any earthquake markers on the map?

  • The Marker objects had not yet been added to the map
  • The drawEarthquake method was not fully implemented in the OceanQuakeMarker and LandQuakeMarker classes.
  • There was no draw() method implemented in the EarthquakeMarker class
  • The EarthquarkeMarker objects had not yet been created

Q3. Which of the following is/are true about the classes used in this programming assignment (SELECT ALL THAT APPLY)?

  • The method call
drawEarthquake(pg, x, y);

in the draw() method in EarthquakeMarker is a call to the helper method drawEarthquake defined and fully implemented in the EarthquakeMarker class.

  • CityMarker is the parent class of EarthquakeMarker
  • The method colorDetermine, which is defined in the EarthquakeMarker class, is what determines the color of both OceanQuakeMarkers and LandQuakeMarkers.
  • The call
super(location);

in the CityMarker’s constructor calls the constructor of SimplePointMarker

  • EarthquakeMarker objects cannot be instantiated using “new”.

Q4. Given the UML class hierarchy you created in step 6 of your programming assignment, which of the following assignment statements WILL NOT cause an error (SELECT ALL THAT APPLY). Assume all of the proper import statements are included at the top of the file.

// Assume the variable feature stores a PointFeature object
EarthquakeMarker em = new OceanQuakeMarker(feature);
 // Assume the variable feature stores a PointFeature object
SimplePointMarker pm = new OceanQuakeMarker(feature);
EarthquakeMarker em = pm; 
  • correct Answer
 // Assume the variable loc stores a Location object
EarthQuakeMarker em = new SimplePointMarker(loc); 
 // Assume the variable feature stores a PointFeature object
Marker m = new OceanQuakeMarker(feature);
SimplePointMarker m = new Marker();
  • correct Answer
 // Assume the variable loc stores a Location object
Object o = new SimplePointMarker(loc);  

Q5. Run your program using the file “quiz1.atom” as the input earthquakesURL. There is a line of code that you can uncomment in setUp that will do this, labeled “uncomment this line to take the quiz”.

How many earthquakes were reported in the United States of America?

  • None
  • 2
  • 18
  • 74
  • 189

Q6. Run your program using the file “quiz1.atom” as the input earthquakesURL. There is a line of code that you can uncomment in setup that will do this, labeled “uncomment this line to take the quiz”.

How many earthquakes were reported to have occurred in the ocean?

  • 30
  • 74
  • 192
  • 200
  • 309

Object Oriented Programming in Java Week 4 Quiz Answers

Quiz: Programming Assignment Quiz (Do programming assignment FIRST)

Q1. Does your program correctly respond to mouse clicks and mouse hovers, as described in the project assignment write up? This should include the following behaviors (test them now):

When I city or earthquake is hovered over, information should be displayed about that city or earthquake. Test this by hovering over several cities and earthquakes now.

It should never be the case that information for more than one item is displayed at the same time. Test this last point explicitly by hovering over two nearby cities or earthquakes, and a city next to an earthquake.

If all earthquakes and cities are displayed, when you click on an earthquake, all other earthquakes should be hidden and all cities except those in the threat circle should be hidden. Test this by clicking on an earthquake now.

If earthquakes or cities are hidden, clicking anywhere should re-display all earthquake and city markers. Test this by clicking somewhere now. Try to test clicking on where a hidden earthquake is to make sure it’s not selected.

If all earthquakes and cities are displayed, when you click on a city, all other cities should be hidden and all earthquakes except those that affect the city (city is in their threat circle) should be hidden. Test this by clicking on a city that has some nearby earthquakes now.

If you click on no marker and everything is already displayed, nothing should happen. Test this now.

  • Yes
  • Not quite…

Q2. Select the classes that are the direct subclasses of (i.e. directly extend) the class CommonMarker in module 5 programming assignment (when the assignment is complete). Select ALL that apply.

  • CityMarker
  • EarthquakeMarker
  • LandQuakeMarker
  • SimplePointMarker
  • OceanQuakeMarker

Q3. What caused the bug in the starter code when you modified CityMarker to extend CommonMarker instead of SimplePointMarker?

  • CommonMarker cannot be extended because it is an abstract class.
  • The header in CityMarker needed to say “implements CommonMarker” instead of “extends CommonMarker”
  • The body of the showTitle method in CityMarker was empty
  • CityMarker did not implement the drawMarker method, which is abstract in CommonMarker.

Q4. What common functionality does CommonMarker implement for its subclasses? In other words, what functionality is inherited by subclasses (not overrided) and used either by users of subclass objects or by the subclass itself? Select ALL that apply.

  • The showTitle method that displays the marker’s text
  • The setClicked method that allows the user to set the “clicked” state of the marker.
  • The draw method
  • The drawMarker method that draws the details of the marker

Q5. Where are the variables mouseX and mouseY, which you used in the method selectMarkerIfHover, declared?

  • They are member variables explicitly declared in EarthquakeCityMap.
  • They are local variables declared in selectMarkerIfHover
  • They are member variables of the PApplet class and are inherited by EarthquakeCityMap
  • They are parameters passed to selectMarkerIfHover

Q6. In which class(es) is the code that actually draws the text label (i.e. the call to the PGraphics text(…) method) next to the city markers and earthquake markers located? Select ALL that apply.

  • EarthquakeMarker
  • LandQuakeMarker
  • CommonMarker
  • OceanQuakeMarker
  • EarthquakeCityMap
  • CityMarker

Q7. Consider the following code, which might be used as a helper method in the mouseClicked method for this programming assignment. This method is supposed to do the following:

  • If lastClicked is not null, it should abort the method.
  • Otherwise it should check all of the cities for a click.
  • If it finds a city that has been clicked and that city is not hidden and that city is the first one it finds to be clicked, it sets lastClicked to be that city’s marker
  • It then sets the rest of the cities to be hidden.
  • If there is no city clicked, it does nothing to the city markers.
private void checkCitiesForClick(){    
if (lastClicked != null)         
return;    
for (Marker marker : cityMarkers)     
{        
if (!marker.isHidden() &&            
marker.isInside(map, mouseX, mouseY) &&            
lastClicked == null)        
{            
lastClicked = (CommonMarker)marker;        
}        
else {            
marker.setHidden(true);        
}    
}   
}
  • There is no bug. The code works correctly as described.
  • It will run the loop to check the cities for a click even if lastClicked is not equal to null.
  • This code can potentially select more than one city, and making it so that more than one city remains unhidden.
  • This code can potentially select a hidden city
  • If no city is clicked, this code will hide all the cities (instead of leaving them alone like it is supposed to).
Get all Course Quiz Answers of Object Oriented Programming in Java Specialization

Course 01: Java Programming: Solving Problems with Software Quiz Answers

Course 02: Java Programming: Arrays, Lists, and Structured Data Quiz Answers

Course 03: Object Oriented Programming in Java Quiz Answers

Course 04: Data Structures and Performance Quiz Answers

Share your love

Newsletter Updates

Enter your email address below and subscribe to our newsletter

Leave a Reply

Your email address will not be published. Required fields are marked *