CSIS 294 Midterm
When a subclass redefines a superclass' method, by using the same signature, the subclass is said to overload that superclass method. +True +False
+False [This is known as overriding, not overloading—an overloaded method has the same name, but a different signature.]
Which of the following is true about inheritance in Java? (Multiple Answers possible) +Protected members are accessible within a package and inherited classes outside the package. +Private methods are final. +Protected methods are final. +We cannot override private methods
+Protected members are accessible within a package and inherited classes outside the package. +Private methods are final. +We cannot override private methods
Which of the following are invalid Java identifiers? XBox $3t4 hop-toad if 2ndFloor MyGoodness
2ndFloor hop-toad if
RuntimeExceptions are Unchecked Exceptions
True
A ______ variable represents classwide information that's shared by all the objects of a class.
static
As of Java SE 8, interfaces can include _______ helper methods.
static
An object's ______ method is called implicitly, when an object appears in code, where a String is needed.
toString toString()
Final methods cannot be overridden and final classes cannot be superclasses.
True
If a superclass declares an abstract method, a subclass must implement that method.
False (Only a concrete sub-class must implement that method.)
Which are not primitive data types supported by the Java programming language? + byte + float + short + Object + word + Array + long + double + int + char + String + boolean
+ Object + word + Array + String
The Java expression 5/2+3 evaluates to +5 +1 +5.5 +0 +none of the above
+5
A has-a relationship is implemented via inheritance. +True +False
+False [A has-a relationship is implemented via composition. An is-a relationship is implemented via inheritance.]
Superclass constructors are not inherited by subclasses. +True +False
+True
A superclass's ________ members can be accessed in the superclass declaration and in subclass declarations. +public +protected +private
+public +protected
What is the output of the following code fragment? int x = 1; while (x<5){ System.out.print(x); if (x==3) { x++; } else { x = x+2; } }
134
If you ran it, what would this program print out? class Shape { protected int vertices; public Shape(final int n) { this.vertices = n; } public int getNumberOfVertices() { return vertices; } public String toString(final String s) { return String.format(s, vertices); } public static void main (String[] args) { System.out.println(new Shape(24).toString("%d")); } }
24
What's a Singleton in Java?
A Singleton in Java is a class that restricts the instances of an object to 1 or a specific amount.
If a class contains at least one abstract method, it's a(n) _____ class
Abstract
Output of following Java program? class Base { public void Print() { System.out.println("Base"); } } class Derived extends Base { public void Print() { System.out.println("Derived"); } } class Main{ public static void DoPrint( Base o ) { o.Print(); } public static void main(String[] args) { Base x = new Base(); Base y = new Derived(); Derived z = new Derived(); DoPrint(x); DoPrint(y); DoPrint(z); } }
Base Derived Derived
This java source compiles without error. (True or False?) class Shape { protected int vertices; public Shape(final int n) { this.vertices = n; } public int getNumberOfVertices() { return vertices; } @Override protected String toString() { return String.format("This shape has %d vertices", vertices); } }
False
What does this program output, if executed? class Except { @Override public String toString() { return this.getClass().getName(); } public static void main(String[]args) { try { System.out.print (new Except()); } catch (Exception e) { System.out.print (" good bye "); System.exit(1); } finally { System.out.print (" hello "); } } }
Except hello
A ClassCastException is a Checked Exception.
False
ALL methods in an abstract class must be declared as abstract methods.
False
_______ is a form of software reusability, in which new classes acquire the members of existing classes and embellish those classes with new capabilities.
Inheritance
The Java compiler produces +an html file +an iterated list +Java bytecode +assembly language +machine language
Java bytecode
Is a static class, with all its members and methods declared static, not just about the same as a singleton? Why do you think you need a Singleton? (If you can think of multiple reasons, still provide only one; the one you consider most important.) Tip: Implement a class with all static members, methods, static initializer and all that. Then implement a singleton in all the different ways you can think of. Now consider, at what point in time, in a typical program lifecycle things would happen ...
No it is not the same, first there is a difference in design. All static methods and members, means that everything is shared between objects of the class, this means there is no restrictions on the amount of instantiations of the objects, only where the data goes. On the other hand, singletons restrict the amount of instantiations, but does not necessarily restrict where the data goes. You might need to use a Singleton when creating a driver for the mouse, there is a restriction on how many mouses you can use.
package edu.gcccd.csis; public class MyEnum { private enum TrafficLight {RED,YELLOW,GREEN} public static void main(final String[] args) { final short k =(short) (System.currentTimeMillis()%3); switch(TrafficLight.values()[k]) { case RED: case YELLOW: System.out.printf("%d : stop ",k); break; default: System.out.printf("%d : drive ",k); } } }
Program prints: "0 : stop" or "1 : stop", or "2 : drive"
An ArithmeticException is an UncheckedException
True
An object of a class that implements an interface, may be thought of as an object of that interface type.
True
Methods that are not interface methods and that do NOT provide implementations, must be declared using the keyword ________.
abstract
Classes from which objects can be instantiated are called ______ classes.
concrete
Casting a reference, stored in a superclass variable to a subclass type is called _______.
downcasting
Keyword ______ specifies that a variable is not modifiable after initialization in a declaration or constructor.
final
As of Java SE 8, any interface containing only one method is known as a(n) ________ interface
functional
What does this program output, if executed? class Except { @Override public String toString() { return (String)((Object)this); } public static void main(String[]args) { try { System.out.println (new Except()); } catch (Exception e) { System.out.println ("good bye"); System.exit(1); } finally { System.out.println ("hello"); } } }
good bye