CSCI 1302 Test 1
What are 3 advantages to using inheritance?
Code that is shared between parent and child classes needs to be written only once. Enhancements to a parent class will automatically be applied to its child classes. It is cost effective to use inheritance
What kind of method are not inherited from a parent class in Java programming?
Constructors
An array index can be a float, double, boolean, or String. T/F
F
What does this code do? for (j=0; j<values.length; j++) values[j]++;
It adds 1 to each element stored in the array
What restriction is there on using the super reference in a constructor?
It must be used in the first statement of a constructor.
If you want to output the text "hi there", including the quote marks, what would you write?
System.out.println("\"hi there\"");
Class Shape is an abstract class. The class Circle is a non-abstract child of Shape. Class shape includes an abstract method drawShape(). The Class Circle must define and implement method drawShape(). T/F
T
If class Animal has a subclass class Mammal, then Mammal can have no other parent than Animal. T/F
T
In Java, constructors do not have a return type. T/F
T
Shadowing should be avoided since it cause unnecessarily confusing code. T/F
T
Are Student and GradStudent proper parent and child classes?
Yes
In Java a variable may contain
a value or a reference
What is the output of the following program segment? int x = 4; for(int i = 1; i <= 5; i+=2) x = x + i; System.out.println(x);
13 (because)
What is the output of following code: class A { public void show() { System.out.println("In Parent"); } } class B extends A { public void show() { System.out.println("In Child"); } } public class Main { public static void main(String[] args) { B ex = new B();; ex.show(); } }
ln Child
What is output with the statement System.out.println("number"+x+y); if x and y are int values where x=10 and y=5?
number105
All classes in Java are directly or indirectly subclasses of the ________ class.
object
The relationship between a class and an object is best described as
objects are instances of classes
Assume that a method has been defined that will compute and return the student's class rank (Freshman, Sophomore, etc). It is defined as: public String getClassRank( ) Given that s1 is a reference to student object, which of the following would properly be used to get s1's class rank?
s1.getClassRank( );
What instance variables can be referenced in class A2? public class A1 { public int x; private int y; protected int z } public class A2 extends A1 { protected int a; private int b; } public class A3 extends A2 { private int q; }
x, z, a, b