Quiz: Final exam
https://sjsu.instructure.com/courses/1364666/quizzes/1373251/take 1/25
Final exam Started: May 13 at 7:13pm
Quiz Instructions Welcome to CS151 section 7 final exam. The final is online to be taken any time during 5/14/20. Use any resources available to you (computer, lecture slides, lecture videos, quiz reviews, search engines). Good luck!
And thank you so much for a great semester! I really enjoyed teaching this course. And thank you for being patient with me teaching this course for the first time! You guys rock!
1 ptsQuestion 1
True
False
In Java collections framework List is an interface
1 ptsQuestion 2
True
False
Classes ArrayList, Vector, and LinkedList are the same data structure but implement data storage in different ways.
1 ptsQuestion 3
Last in, first out (LIFO)
Stack is what type of data structure?
5/13/2020 Quiz: Final exam
https://sjsu.instructure.com/courses/1364666/quizzes/1373251/take 2/25
Always a vector of integers
Ordered list
First in, first out (FIFO)
1 ptsQuestion 4
True
False
LinkedList data structure in Java collections is implemented as doubly linked lists.
1 ptsQuestion 5
Based on the lowest priority (element with the lowest priority is at the front of the queue)
Always in the most recent element added (LIFO data structure)
Always in the least recent element added (FIFO data structure)
Based on the highest priority (element with the highest priority is at the front of the queue)
In PriorityQueue data structure how are the elements placed at the front of the queue?
1 ptsQuestion 6
FIFO data structure
LIFO data structure
What type of data structure is a set?
5/13/2020 Quiz: Final exam
https://sjsu.instructure.com/courses/1364666/quizzes/1373251/take 3/25
Ordered list
Unordered list
1 ptsQuestion 7
Geographic data
Unique values, referenced by keys
Key-value pairs
Tabulated data
Classes that implement Map interface in Java collections framework are used for storing what type of data?
1 ptsQuestion 8
True
False
“Multithreading” and “concurrent programming” are synonymous terms.
1 ptsQuestion 9
Thread ID 1
Initial thread
Main thread
Parent thread
When your application starts what is the name of the special thread that starts as well?
5/13/2020 Quiz: Final exam
https://sjsu.instructure.com/courses/1364666/quizzes/1373251/take 4/25
1 ptsQuestion 10
Can execute in the background but have to completed before the main thread terminates
Threads run by the system during your application execution
Java doesnt wait for it to finish executing before it terminates main thread
Highest priority threads
What are daemon threads?
1 ptsQuestion 11
Implement Runnable interface
Implement Thread interface
Extend MainThread class
Extend Thread class
Just implement run() method without changing the class header
Implement Synchronized interface
What are the ways you can make an instance of your class be able to start a thread? In other words, what can you add to your class definition to mark it as being capable to start a thread? Multiple answers should be selected for this question.
1 ptsQuestion 12
What is the default priority of every thread when your application starts?
5/13/2020 Quiz: Final exam
https://sjsu.instructure.com/courses/1364666/quizzes/1373251/take 5/25
5
1 ptsQuestion 13
True
False
Deadlocks will resolve themselves if you give it time and wait (choose if this statement is True or False).
1 ptsQuestion 14
Adding transient keyword
Synchronized method
Changing thread priorities for each thread to ensure they are scheduled for execution sequentially
Efficient thread communication
Concurrent blocks
Concurrent methods
Static synchronization
Synchronized block
What are some of the ways to achieve thread locks in Java? Please select multiple answers.
1 ptsQuestion 15
Which component of JVM is responsible for loading a class?
5/13/2020 Quiz: Final exam
https://sjsu.instructure.com/courses/1364666/quizzes/1373251/take 6/25
Method area
Heap memory
Execution engine
Class loader subsystem
1 ptsQuestion 16
Only other members of the class can see this variable/method
Allows unrestricted access to the variable/method
Members of this class and other classes within the package can see this variable/method
Private classes are visible to other classes in the same .java file
Only other members of the class and its children can see this variable/method
“Private” access modifier indicates which level of visibility?
1 ptsQuestion 17
Composition
Abstraction
Overloading
Inheritance
A person has an address attribute. The address is represented by a class named Address (the type of this attribute is Address). What kind of relationship is this?
1 ptsQuestion 18
5/13/2020 Quiz: Final exam
https://sjsu.instructure.com/courses/1364666/quizzes/1373251/take 7/25
Only executed when default constructor is invoked and executed before the class variables are initialized
Always executed after the body of the constructor is executed
You have to tell the compiler to execute instance initializer block or it will not be executed
Always executed before the body of the constructor is executed
When is instance initializer block executed?
1 ptsQuestion 19
Do not provide getters
All attributes have to be declared static
Rely on default constructor
Only provide parameterized constructor
Do not provide setters
Class has to be declared final
All fields in the class have to be declared final
To make an immutable class what do you need to do? Select multiple answers as there are multiple steps you need to perform in order to make a class to be immutable.
1 ptsQuestion 20
Java API
What are the four core principles of object oriented design? Select multiple answers that correspond to these principles.
5/13/2020 Quiz: Final exam
https://sjsu.instructure.com/courses/1364666/quizzes/1373251/take 8/25
Composition
Public interfaces
Encapsulation
Tight coupling
Inheritance
Polymorphism
Utility classes
Abstraction
1 ptsQuestion 21
Abstraction
Encapsulation
Dynamic polymorphism
Loose coupling
Static polymorphism
Method overloading corresponds to which of object oriented design principle?
1 ptsQuestion 22
No
Yes
In Java when overriding a parent method, can the method in the child class have additional input parameters?
5/13/2020 Quiz: Final exam
https://sjsu.instructure.com/courses/1364666/quizzes/1373251/take 9/25
1 ptsQuestion 23
runtime
both compile time and runtime
compile time
Exception handling allows catching issues with the program during …
3 pts
checked exceptions compiler checks if these exc
unchecked exceptions compiler does not check if t
errors are not handled by exceptio
try-catch block a block of code in which exc
throw allows programmatically th
throws allows delegating exception
finally block always executes upon termi
Question 24
Please match these terms with the correct explanations
1 ptsQuestion 25
5/13/2020 Quiz: Final exam
https://sjsu.instructure.com/courses/1364666/quizzes/1373251/take 10/25
True
False
Java allows catching multiple exception types in the same try-catch block.
1 ptsQuestion 26
True
False
Enum data structures are internally compiled into classes.
1 ptsQuestion 27
Yes
No
Can you instantiate an instance of List objects in the following way?
List
1 ptsQuestion 28
No
Yes
Is an instance of ArrayList also Iterable type?
5/13/2020 Quiz: Final exam
https://sjsu.instructure.com/courses/1364666/quizzes/1373251/take 11/25
1 ptsQuestion 29
Yes
No
Is Vector data structure synchronized?
1 ptsQuestion 30
Java GameFX
Java Animation
JAVA Swing
JavaFX
Java Graphics
Java AWT API
Java UIKit
Which Java GUI programming frameworks did we cover in this class?
1 ptsQuestion 31
True
False
You can implement custom listeners in Java AWT.
1 ptsQuestion 32
5/13/2020 Quiz: Final exam
https://sjsu.instructure.com/courses/1364666/quizzes/1373251/take 12/25
Adapter classes allow us to only implement those listener events which we are interested in handling
These classes implement listener interfaces
Adapter classes force you to implement all event handlers they inherit from listener interfaces
Adapter classes allow handling special/custom events.
Adapter classes is how you implement custom listeners
What are adapter classes in Java AWT and Java Swing? Note that several of the statements below are true.
1 ptsQuestion 33
True
False
Content pane in Java Swing is internally implemented as a JPanel.
1 ptsQuestion 34
True
False
Radio buttons in Java Swing are automatically grouped into a button group without a need to implement such group, simply based on them being radio buttons and not checkboxes.
5 ptsQuestion 35
5/13/2020 Quiz: Final exam
https://sjsu.instructure.com/courses/1364666/quizzes/1373251/take 13/25
?HTML Editor
In my program I instantiate a list of fruits (see below). Implement a code snippet that iterates over this list and prints out each fruit info to a command line. Assume that each fruit class has toString() method implemented and you can simply call that method on each fruit instance in your System.out.println() call (e.g. System.out.println(myFruitInstance.toString() ). Use your favorite iteration method for this question. My lecture slides show at least 6 different ways to iterate over elements of a collection. Please only include the block of code that directly relates to iterating over the elements of the collection. Do not implement the entire application or copy the code which instantiates the collection and adds elements to it.
// … some code before this
List
myFruits.add(new Apple(“I am an apple”));
myFruits.add(new Orange(“I am an orange”));
myFruits.add(new Pear(“I am a pear”));
myFruits.add(new Apple(“I am an apple”));
myFruits.add(new Pear(“I am a pear”));
myFruits.add(new Plum(“I am a plum”));
// iterate over the elements of myFruit collection and call toString() on each element to print element info to the command line (don’t forget to use System.out.println() method for this):
…
// … some code after this
? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ? ? 12pt Parag
5/13/2020 Quiz: Final exam
https://sjsu.instructure.com/courses/1364666/quizzes/1373251/take 14/25
0 words0 words ?
5 ptsQuestion 36
Please see slide 35 in the Java collections lecture module (example of using custom comparator with the PriorityQueue data structure to sort employees based on their ranking). Please change the implementation of the comparator to rank employees with the lowest rank to have the highest priority. In the example of the output given on that slide Joe, who has the lowest rank, will then be at the top of the PriorityQueue data structure. Please only include the code for the custom comparator into your answer:
Comparator
Below is the code for the original example for your convenience:
import java.util.*; import java.lang.Math.*;
class Employee{ private String name; private double performanceRank;
Employee(String name, double rank){ this.name = name;
5/13/2020 Quiz: Final exam
https://sjsu.instructure.com/courses/1364666/quizzes/1373251/take 15/25
?HTML Editor
this.performanceRank = rank; }
public double getRank(){return this.performanceRank;}
@Override public String toString(){ return this.name+” (“+this.performanceRank+”)”; } }
public class Test { public static void main(String[] args){ Comparator
PriorityQueue
// queue elements: names.add(new Employee(“Jeff”, 1.1)); names.add(new Employee(“Dan”, 1.9)); names.add(new Employee(“Joe”, 0.5)); names.add(new Employee(“Annie”, 2.7)); names.add(new Employee(“Casey”, 3.1)); names.add(new Employee(“Anthony”, 1.6)); names.add(new Employee(“Alan”, 2.5));
Iterator nameIterator = names.iterator(); while(nameIterator.hasNext()){ System.out.println(nameIterator.next()); } } }
? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ? ? 12pt Parag
5/13/2020 Quiz: Final exam
https://sjsu.instructure.com/courses/1364666/quizzes/1373251/take 16/25
0 words0 words ?
5 ptsQuestion 37
Please see slide 21 of the “Multi-threading and concurrent programming” lecture module. The example in this slide demonstrates thread priority inheritance. Please modify this example to use Runnable interface instead of Thread class. Include the code of the whole modified program into your answer.
Below is the code for the original example for your convenience:
import java.lang.*;
public class Test extends Thread{ public void run() { System.out.println(“Executing run() method”); }
public static void main(String[]args) { Thread.currentThread().setPriority(8); System.out.println(“main thread priority is ” + Thread.currentThread().getPriority());
Test myTest = new Test(); System.out.println(“myTest thread priority is ” + myTest.getPriority()); } }
5/13/2020 Quiz: Final exam
https://sjsu.instructure.com/courses/1364666/quizzes/1373251/take 17/25
?HTML Editor
? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ? ? 12pt Parag
0 words0 words ?
10 ptsQuestion 38
I have a class named Counter. In this class I have a synchronized method called countRange(). This method prints out a count in the range from the minimum to the maximum values stored by the Counter class attributes. Please implement the body of the main() method in Test class to instantiate and start two threads that invoke countRange() method. You can use either named or anonymous Thread instances. Don’t forget to make a call to start() method in each thread. What I am asking you to do is similar to what Stopwatch examples in the “Multi-threading and concurrent programming” lecture module showed.
Please only include the code inside the main() method into your answer.
Below is the implementation of Counter class and the shell of the main() method in Test class:
import java.lang.*;
class Counter { private int min; private int max;
Counter(int min, int max){ this.min = min;
5/13/2020 Quiz: Final exam
https://sjsu.instructure.com/courses/1364666/quizzes/1373251/take 18/25
?HTML Editor
this.max = max; }
synchronized public void countRange() { for(int i = this.min; i <= this.max; i++){ System.out.println(i); } } }
public class Test { public static void main(String[]args) { // your implementation goes here } }
? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ? ? 12pt Parag
0 words0 words ?
10 ptsQuestion 39
I have a Java Swing GUI application that displays a single button and when this button is pressed a random caption/text for the button is generated and displayed on the button. Below is almost complete implementation of this program but there is no button functionality implemented yet. Please implement this button's
5/13/2020 Quiz: Final exam
https://sjsu.instructure.com/courses/1364666/quizzes/1373251/take 19/25
functionality by adding an appropriate listener to this button (named myButton). Remember that you can add a named listener instance as well as anonymous listener instance, that is your choice. I suggest you refer to slide 23 of the GUI lecture module (remember that event handling in Java AWT and Java Swing is the same). Please test your code by compiling and executing before submitting your answer.
The body of the method inside event listener will simply update the text on the button as follows:
myButton.setText(Test.generateLabel());
Note that occasionally the random text generator produces the same text as what is currently displayed on myButton.
Below is the implementation of the application with the exception of event handling for myButton:
import java.awt.event.*; import javax.swing.*; import java.util.Random; import java.util.*;
public class Test extends JFrame { public static String generateLabel(){ List
Random random = new Random(); int randomInteger = random.nextInt(labels.size()); return labels.get(randomInteger); }
public Test(){ setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setTitle(“Test app”); setSize(300,200); setLocation(10,200);
JPanel content = new JPanel(); JButton myButton = new JButton(“Test button”);
5/13/2020 Quiz: Final exam
https://sjsu.instructure.com/courses/1364666/quizzes/1373251/take 20/25
?HTML Editor
content.add(myButton); setContentPane(content);
// your code for handling button click event goes here }
public static void main(String[]args) { JFrame myApp = new Test(); myApp.show(); } }
? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ? ? 12pt Parag
0 words0 words ?
5 ptsQuestion 40
Please see the example of generic method that uses multiple type parameters on slide 13 of the “Java Generics” lecture module. The method printKeyValue() accepts two arguments of types K and V and prints them to command line. Please modify this example to add a third type parameter named D and a third input argument of that type (named descriptionObject) and print that additional argument as follows:
5/13/2020 Quiz: Final exam
https://sjsu.instructure.com/courses/1364666/quizzes/1373251/take 21/25
?HTML Editor
System.out.print(keyObject+”: “+valueObject+”, “+descriptionObject);
Then invoking printKeyValue() method would change to something like this:
printObject(“StringKey”,101,”new third argument”);
Below is the code in the example for your convenience:
public class Test { private static < K, V > void printObject(K keyObject, V valueObject) { System.out.print(keyObject+”: “+valueObject); }
public static void main(String[] args){ printObject(“StringKey”,101); System.out.println();
printObject(10,”this is my value”); System.out.println();
printObject(“StringKey”,10.5); System.out.println();
printObject(12,1.2); System.out.println(); } }
? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ? ? 12pt Parag
5/13/2020 Quiz: Final exam
https://sjsu.instructure.com/courses/1364666/quizzes/1373251/take 22/25
0 words0 words ?
5 ptsQuestion 41
Please see slide 20 in “Java reflection” lecture module for an example of listing all methods and constructors in class Employee. In this example we instantiate an instance of class Employee and name it e1. We then call getClass() method on that Employee instance to get the class information for Employee class and store it in a Class type variable named employeeClass. Please change the following line to use forName() method to get the Employee class information:
Class employeeClass = e1.getClass();
Please remember that you would use forName() method in a different way than getClass() method. See examples in that lecture module slides for information about how to use forName() method.
Please only include the changed line of code as your answer for this question. Do not include the entire example code.
Below is the code in the example for your convenience:
class Employee{ private String name; private int id; private String employer;
Employee(){ this.name = “”; this.id = 0; this.employer = “”; }
Employee(String name, int id, String employer){ this.name = name; this.id = id; this.employer = employer; }
public String getName(){return this.name;} public int getID(){return this.id;} public String getEmployer(){return this.employer;}
5/13/2020 Quiz: Final exam
https://sjsu.instructure.com/courses/1364666/quizzes/1373251/take 23/25
?HTML Editor
public void setName(String name){this.name = name;} public void setID(int id){this.id = id;} public void setEmployer(String employer){this.employer = employer;}
public String toString(){ return this.name + ” (” + this.id + “) at “+this.employer; } }
public class Test { public static void main(String args[]){ Employee e1 = new Employee(“Janet Donovan”, 101, “Company2”); System.out.println(“My employee: “+e1.toString());
Class employeeClass = e1.getClass();
System.out.println(“nEmployee class methods:”); Method[] employeeMethods = employeeClass.getMethods(); for(Method method : employeeMethods){ System.out.println(method.getName()); }
System.out.println(“nEmployee class declared methods:”); Method[] employeeDeclaredMethods = employeeClass.getDeclaredMethods(); for(Method method : employeeDeclaredMethods){ System.out.println(method.getName()); }
System.out.println(“nEmployee class declared constructors:”); Constructor[] employeeDeclaredConstructors = employeeClass.getDeclaredConstructors(); for(Constructor method : employeeDeclaredConstructors){ System.out.println(method.getName()); } } }
? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ? ? 12pt Parag
5/13/2020 Quiz: Final exam
https://sjsu.instructure.com/courses/1364666/quizzes/1373251/take 24/25
0 words0 words ?
10 ptsQuestion 42
Please refer to various examples for how to instantiate a new instance of a class using Java reflection. These examples can be found in slides 42 – 46 in “Java reflection” lecture module. Imagine we have a class named Dog and we want to instantiate a new instance of this class from our code. Name the new instance “dog1”. Please use one of the several ways to instantiate an instance of a class presented in the lecture slides and write code that creates an instance of Dog class.
class Dog {
private String name;
private Double weight;
public Dog() {
this.name = “Unknown”;
this.weight = 0.0;
}
public String getName() {return this.name;}
public Double getWeight() {return this.weight;}
public void setName(String n) {this.name = n;}
5/13/2020 Quiz: Final exam
https://sjsu.instructure.com/courses/1364666/quizzes/1373251/take 25/25
Quiz saved at 7:41pm
?HTML Editor
public void setWeight(Double w) {this.weight = w;}
}
public class Test {
public static void main(String args[]){
// your code goes in here; create a new instance of Dog class, call it “dog1”
}
}
? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ? ? 12pt Parag
0 words0 words ?
Submit Quiz
Applied Sciences
Architecture and Design
Biology
Business & Finance
Chemistry
Computer Science
Geography
Geology
Education
Engineering
English
Environmental science
Spanish
Government
History
Human Resource Management
Information Systems
Law
Literature
Mathematics
Nursing
Physics
Political Science
Psychology
Reading
Science
Social Science
Home
Blog
Archive
Essay
Reviews
Contact
google+twitterfacebook
Copyright © 2019 HomeworkMarket.com
