Midterm Practice
Given the following class diagram for MCCStudent (MiraCosta College Student), please write the code for the MCCStudent class: MCC Student Class Diagram public class MCCStudent { // 1) Declare all 4 fields // 2) Implement the 2 constructors, as follows: // a) public MCCStudent(String name, String major, double gpa, int unitsCompleted) // This constructor should initialize all fields to the values of the parameters // b) public MCCStudent(String name) // This constructor should initialize the field name to the name parameter, but major set to "undecided", gpa = 4.0 and unitsCompleted = 0. // Be sure to call/reuse the first constructor
//code
Given the following class diagram for the 3 classes: Food, Vegetable and Meat, please write the class definitions for the Food class (parent) and only 1 of the children (your choice: Vegetable or Meat). FoodInheritanceClassDiagram.png public class Food { // 1) Declare all 2 fields // 2) Implement the 2 constructors, as follows: // a) public Food(String name, int calories) // This constructor should initialize all fields to the values of the parameters // b) public Food(String name) // This constructor should initialize the field name to the name parameter, but set calories=0 // Be sure to call/reuse the first constructor (2 parameters) from the second constructor (1 parameter) for full-credit. // Do not code anything other than the fields and 2 constructors! } Now implement only 1 of the child classes (Vegetable or Meat). Be sure to inherit from Food, declare the 1 field and write only 1 constructor for the child class that reuses the constructor from the parent class.
//code
A binary tree has exactly _________ link instance variables. 0 1 2 3 4
1
What is the post-order traversal of the following binary search tree? 50 17 72 12 23 54 76 9 14 19 67 50, 17, 12, 9, 14, 23, 19, 72, 54, 67, 76 9, 14, 12, 17, 19, 23, 50, 54, 67, 72, 76 9, 14, 12, 19, 23, 17, 67, 54, 76, 72, 50 9, 12, 14, 17, 19, 23, 50, 54, 67, 72, 76
9, 14, 12, 19, 23, 17, 67, 54, 76, 72, 50
When defining recursive valued methods you should: Ensure there is no infinite recursion. Ensure each stopping case returns the correct value for that case. Ensure that the final value returned by the method is the correct value. All of the above
All of the above
Explain the differences between a text file, an ASCII file and a binary file.
An ASCII file is read as a file similar to one with strings, while a binary file has numbers that can be processed as 1's and 0's. ASCII file's are text files and are harder to process than binary files.
A generic class can be instantiated as any of the following base types except: [choose all that apply - there may be more than one correct answer] Character Array String Double char double
Array, char, double
A/An ____________ can change the length while the program is running. Array ArrayList Generic None of the above
ArrayList
______ refers to the process of associating a method definition with a method invocation. Binding Encapsulation Inheritance Polymorphism
Binding
The _____________ manager places components into five regions. BorderLayout FlowLayout GridLayout AbsoluteLayout
BorderLayout
There are two common classes used for reading from a text file. They are: PrintWriter and BufferedReader FileInputStream and Scanner BufferedReader and Scanner ObjectInputStream and ObjectOutputStream none of the above
BufferedReader and Scanner.
Exceptions that are subject to the catch or declare rule are called: Checked exceptions Unchecked exceptions Errors all of the above
Checked exceptions
A programming style that uses a signal and response approach to programming is called: Object Oriented Programming Structured Programming Functional Programming Event-Driven Programming Bottom-Up Programming Top-Down Programming
Event-Driven Programming
Binary files store data in the same format that is used by any common text editor. True False
False
In Java, when you open a text file you should account for a possible: FileNotFoundException InputMismatchException NullPointerException all of the above
FileNotFoundException
The _____________ manager is the simplest layout manager. It arranges components one after the other, going from left to right, in the order in which you add them. BorderLayout FlowLayout GridLayout SimpleLayout AbsoluteLayout
FlowLayout
Java contains a mechanism that automatically reclaims memory. This mechanism is called: Garbage elimination Recycling objects Garbage collection System collection
Garbage collection
The method paint has a parameter(s) of type: [Choose all that apply - there may be more than one correct answer] Shape Canvas Graphics Image String
Graphics
The _______________ manager arranges components in a two-dimensional table with some number of rows and columns. BorderLayout FlowLayout GridLayout AbsoluteLayout
GridLayout
Composition indicates a/an _______________ relationship. Is-a Has-a all of the above none of the above
Has-a
What are two advantages to using inner classes?
Inner classes have access to all variables/data from outer classes. Inner classes are also easier to understand and help with code optimization.
Inheritance indicates a/an ____________ relationship. Is-a Has-a all of the above none of the above
Is-a
All recursive methods have a/an_____ equivalent method Iterative Selective Declarative Inherited None of the above
Iterative
The arrowheads in an inheritance diagram point: (a) North (b) South (c) East (d) West
North
A common exception that occurs when using linked lists is the: ArrayIndexOutOfBoundsException IndexOutOfBoundsException NodeOutOfBoundsException NullPointerException
NullPointerException
Methods that process String arguments as if they were numbers could possibly throw a/an _________ exception. NumberFormatException NullPointerException both a and b none of the above
NumberFormatException
In Java, a node is a/an: String Integer Object Exception
Object
During recursion, if the stack attempts to grow beyond its limit, a _____________ occurs. Stack underflow Stack overflow Recursive underflow Recursive overflow
Stack overflow
What is the function of the variable head when used with a linked list? What is the data type of the head variable?
The function of a variable head when used with a linked list is used to reference to the Head Node but is not the actual head node. It is used to locate the first node of the list. The data type of the head is an String and a non primitive type.
Explain the Java coordinate system for graphic objects.
The java coordinate system for graphics objects is a "window" with pixels that uses an x-y coordinate system to plot points. It starts at the top left corner where x moves the coordinate right and y moves the coordinate down.
What is the the purpose of the Iterator data structure?
The purpose of an Iterator data structure is to process every element of a container and it can process an entire collection at one time.
Binary files can be handled more efficiently than text files. True False
True
The preferred stream classes for processing binary files are ObjectInputStream and ObjectOutputStream. True False
True
A runtime exception is a/an: checked exception unchecked exception error none of the above
Unchecked Exception
The Java standard libraries have a class named ___________ that behaves almost exactly the same as the class ArrayList. Array Container Vector All of the above None of the above
Vector
In Java, an inner class is: a concrete class an abstract class a class declared within another class a completely abstract class with empty methods
a class declared within another class
In Java, an interface is: a concrete class a completely abstract class with empty methods any code with graphics a class declared within another class
a completely abstract class with empty methods
The Graphics class is a(n) ____________ class. discrete concrete abstract none of the above
abstract
You cannot create an object using a/an: superclass constructor subclass constructor ancestor class constructor abstract class constructor
abstract class constructor
A button fires events known as: static events passive events dynamic events button events action events
action events
The ActionListener interface requires that the method ____________ be implemented. actionEvent() actionFired() actionRegistered() actionPerformed()
actionPerformed()
Icons can be added to: JLabels JButtons JMenuItems all of the above
all of the above
The stream that is automatically available to your Java code is: System.out System.in System.err all of the above
all of the above(System.out, System.in, System.err)
The principals of object oriented programming include: (a) encapsulation (b) inheritance (c) polymorphism (d) all of the above
all of the above(encapsulation, inheritance, polymorphism)
1) The new operator: (a) allocates memory (b) is used to create an object of a class (c) associates an object with a variable that names it. (d) All of the above.
all of the above.(allocates memory, is used to create an object of a class, associates an object with a variable that names it.)
File whose contents must be handled as sequences of binary digits are called text files ASCII files input files output files binary files
binary files.
The hasNext() method of the Iterator interface has a return value of ______________.
boolean
The remove(Object o) method of the ArrayList class returns: int double byte boolean Object
boolean
A node contains: data item(s) reference(s) to another node both A and B none of the above
both A and B
The second section of a UML class diagram specifies the: (a) class members (b) class name (c) class methods (d) class modifiers
class members
The third section of a UML class diagram specifies the: (a) class members (b) class name (c) class methods (d) class modifiers
class methods
The first section of a UML class diagram specifies the: (a) class members (b) class name (c) class methods (d) class modifiers
class name
All of the following are methods of the Iterator interface except: clear() hasNext() next() remove()
clear()
All of the following are methods of the Graphics class except: drawRect fillRect coloredRect fill3DRect
coloredRect
A class with no abstract methods is called a abstract class private class concrete class protected class
concrete class
Assigning an object of an ancestor class to a descendent class is called: static binding dynamic binding upcasting downcasting
downcasting
A program whose only task is to test a method is called a: (a) driver program (b) stub (c) bottom-up test (d) recursive method
driver(a demo) program
Making the Node class a private inner class of a linked data structure is an example of: polymorphism encapsulation inheritance all of the above
encapsulation
The keyword ___________ can be used to place a bound on a type parameter. implements inherits encapsulates instanceof extends
extends
A____block executes regardless of whether an exception occurs. catch finally decision none of the above
finally.
The method_______ from the File class forces a physical write to the file of any data that is buffered. close() flush() writeUTF() writeObject()
flush()
Class and method definitions that include parameters for types are called: generics ArrayList Collections Data Types
generics
All of the following are methods of the File class except: exists() delete() getDirectory() getAbsolutePath()
getDirectory()
Which of the following are methods of the class Color? [Select all that apply - there may be more than one correct answer] getRed brighter darker dimmer setColor getColor
getRed, brighter,darker,setColor,getColor
The scanner class has a series of methods that checks to see if there is any more well-formed input of the appropriate type. These methods are called __________ methods: nextToken hasNext next getNext testNext
hasNext
The first node in a linked list is commonly referred to as the ________ node. head tail null zero
head
A common use of inner classes is: clean up set up helper all of the above
helper
A class that uses an interface must use the keyword: extends Cloneable Serializable extends implements
implements
Recursively visiting the left subtree, root node and then the right subtree describes: pre-order processing Correct Answer in-order processing root processing post-order processing none of the above
in-order processing
Regarding recursion, if a base case is never reached the result is: infinite recursion iteration termination all of the above
infinite recursion
When defining your own exception class, you extend an existing exception class. This is an example of: polymorphism encapsulation inheritance all of the above
inheritance
An ___________ allows data to flow into your program. input stream output stream file name exception all of the above
input stream
A variable whose meaning is confined to an object of a class is called: (a) instance variable (b) local variable (c) global variable (d) none of the above
instance variable.
The read() method of the class RandomAcessFile returns the type: byte int char double
int
A recursive method is one that: Returns a value Initializes a set of variables Returns no value Invokes itself with smaller input
invokes itself with smaller input.
All of the following are memory management methods of the ArrayList class except: ensureCapacity() clear() isEmpty() remove() trimToSize()
isEmpty()
All of the following are search methods of the ArrayList class except: isEmpty() lastIndexOf() indexOf() contains()
isEmpty()
Standard layout managers are defined in the ____________ package. javax.swing java.util java.awt java.lang
java.awt
The Insets class is in the package ______________. java.awt javax.swing javafx java.util java.lang
java.awt
The exception class belongs to the package: java.io java.util java.lang java.exception
java.lang
To use the Java Iterator Interface you must import the ____________ package. java.text java.LinkedList java.iterator java.util java.tools
java.util
To use the Java class ArrayList, you must import the package: java.awt java.ArrayList java.util java.collection java.container
java.util
Which of the following are NOT methods of the ArrayList class? [Choose all that apply - there may be more than one correct answer] length() size() get() set() capacity()
length(), capacity()
A button component should have a registered _____________ associated with it. pixel layout JFrame listener none of the above
listener
A variable whose meaning is confined to a method definition is called an/a (a) instance variable (b) local variable (c) global variable (d) none of the above
local variable.
All of the following are methods of the Scanner class except: nextFloat() next() nextChar() nextLine()
nextChar()
In Java, you indicate the end of a linked list be setting the link instance variable of the last node in the linked list to __________. 0 -1 1 null NULL
null
UML is a graphical language that is used for designing and documenting software created within the _____________ framework. (a) top-down programming (b) bottom-up programming (c) structured programming (d) object oriented programming
object oriented programming
In Java, a derived class can have _____ base class(es). one two three no limit
one
The catch block has ________ parameters. zero one two three
one
If you choose to implement the equals method in your code, you must ___________ the equals method from the Object class. override overload encapsulate protected
override.
In a UML class diagram, the tilde (~) indicates: (a) public access (b) protected access (c) private access (d) package access
package access
Recursively visiting the root node, left subtree and then the right subtree describes: pre-order processing in-order processing root processing post-order processing none of the above
pre-order processing
In Java, call-by-value is only used with: (a) objects (b) primitive types (c) this (d) all of the above
primitive types
The class ObjectOutputStream contains all of the following methods except: writeInt() writeChar() writeDouble() println()
println()
An abstract method cannot be modified by: public protected private none of the above
private
In a UML class diagram, the minus (-) indicates: (a) public access (b) protected access (c) private access (d) package access
private access
Inner classes available outside the scope of their outer class are modified by the keyword: public private protected package Cloneable
protected
In a UML class diagram, the sharp (#) indicates: (a) public access (b) protected access (c) private access (d) package access
protected access
An interface and all of its method signatures are normally declared to be: public private protected package private
public
In a UML class diagram, the plus (+) indicates: (a) public access (b) protected access (c) private access (d) package access
public access
Write the code to declare a new exception class named ConfusingQuestionException that sends the message "This question is too confusing!" to the constructor of its parent class Exception. Your code should include a declaration of the class (ConfusingQuestionException) and a constructor (no parameters) that sends the message "This question is too confusing!" to its parent class.
public class ConfusingQuestionException extends Exception{ public class ConfusingQuestionException(){ super("This question is too confusing!"); } }
The method that must be implemented in a class using the Comparable interface is: public Object compareTo(Object other) public boolean compareTo(Object other) public int compareTo(Object other) none of the above
public int compareTo(Object other)
The method _________ reads a single character from an input stream. readLine() next() read() nextChar()
read()
The class ObjectInputStream all of the following methods expect: readLine() readChar() readObject() readInt()
readLine()
A recursive solution can be preferable to an iterative solution because: recursive method calls are faster than iterative looping recursive solutions may be easier to understand than iterative ones recursion uses less memory than iteration iteration should be avoided.
recursive solutions may be easier to understand than iterative ones
Any change to graphics being displayed on the screen requires a call to the method ____________ to update the graphics. paint repaint update all of the above
repaint
Inheritance promotes code ___________. (a) redundancy (b) reuse (c) refactoring (d) repeats (e) all of the above
reuse
The _________ node is the first node in the tree data structure. leaf parent child root sibling branch
root
A _____________ copy of an object is a copy that has references in common with the original object. bit copy deep copy shallow copy singular copy
shallow copy
The underlying data structure used by the computer during recursion is a: array queue stack tree linked list
stack
All recursive methods must have a/an: starting case intermediate case stopping case none of the above
stopping case
The parameter this refers to (a) instance variables (b) local variables (c) global variables (d) the calling object
the calling object
Try blocks contain code that could possibly: handle an exception throw an exception catch an exception display an exception
throw an exception
Two methods that are expected to be in all Java classes are: (a) getName and setName (b) toString and equals (c) compareTo and charAt (d) toLowerCase and toUpperCase
toString and equals
Assigning an object of a derived class to a variable of a base class is called: static binding dynamic binding upcasting downcasting
upcasting
An invocation of the Container class method _____________ updates components on the screen. setVisible setLayout update validate
validate