mTEST 1 – MIDTERM – 1) What is the value of names[4] in the following array? Str
ID: 3839140 • Letter: M
Question
mTEST 1 – MIDTERM –
1) What is the value of names[4] in the following array?
String[] names - {"Jeff", "Dan", "Sally", "Jill", "Allie"};
a) Sally
b) Allie (CORRECT)
c) Jill
d) None
2) Values held by attributes of an object describe the object's
a) identity (CORRECT)
b) operation
c) state
d) class
3) Which of the following can contain multiple entries and can grow its size/length
automatically?
a) Array (CORRECT)
b) HashMap
c) String
d) StringBuilder
4) Which keyword is used to indicate that a class cannot be extended through inheritance
a) abstract
b) final (CORRECT)
c) public
d) static
5) An abstract class cannot
a) be instantiated
b) have abstract methods
c) be used as a superclass
d) have fields/attributes (CORRECT)
6) What is wrong with the following constant declaration?
final int count = "2";
a) type int is not valid for final designation
b) count is a Java reserved keyword and cannot be used as an identifier (CORRECT)
c) count must be assigned an integer value
d) visibility is not provided for the constant.
7) What value is printed for the following statement?
int n = 0;
for (int i=1; i < 6; i = i + 2)
{ n = n + 1; }
System.out.println(n);
a) 2
b) 3 (CORRECT)
c) 4
d) 5
8) What happens when this statement is executed?
Automobile car = new Automobile (1);
a) An object of the Automobile class is created. (CORRECT)
b) An object of the Automobile class is declared, but not created.
c) A constructor that accepts a string in the Automobile class is called.
d) The default constructor in the Automobile class is called.
9) A methods signature is defined by which of the following?
a) method name only
b) method name, number of arg, and arg types
c) method name, number of arg, abd arg types and sequences
d) method name, number of arg, arg types and sequences, and method return type
(CORRECT)
10) The maximum number of interfaces which can be implemented by a class is
a) zero
b) one
c) two
d) more than two (CORRECT)
11) Two or more methods in the same class may have the same name as long as ___ are different.
a) their return values (CORRECT)
b) their signatures
c) the statements within their curley braces
d) their access specifiers
12) Where an object is passed as an argument for a method _________ is actually passed.
a) a copy of the object
b) a reference to the object
c) the name of the object
d) none of the following
13) What keyword do you use in class declaration to create a subclass?
a) extends (CORRECT)
b) overrides
c) inherites
d) overloads
14) Which of the following statements is false
a) All the methods in an interface are abstract
b) All interface can contain static constraints
c) A class can inherit another class and all the same time implement an interface
d) A Class can implement just the methods of an interface it needs instead of all methods of an interface.
15) To write a file more efficiently you should use a
a) FileReader stream
b) BufferedWriter stream
c) Filestream
d) FileWriter stream (CORRECT)
16) Which method transmits any remaining data from the buffer without closing the stream?
a) close()
b) flush()
c) send()
d) stop()
17) Assume x is an integer number what is the highest index value in the following array?
byte[] values = new byte[x];
a) 0
b) x-1
c) x
d) x+1
18) To change the size of an ArrayList, you must
a) code a stateent that specified the new number of elements
b) code a statement that specifies the percent by which you want the size increased
c) either a or b
d) none of the above ( CORRECT )
19) What is the value of s after the following code is executed?
ArrayList<String> inventory = new ArrayList<String>();
inventory.add("B");
invetory.add("C");
inventory.add("D");
inventory.add(1,"E");
String s = inventory.get(2);
add(E e): Appends the specified element to the end of this list
add(int index, E element): inserts the specified element at the specified position in this list.
a) "B"
b) "C"
c) "D"
d) "E"
20) When the code within a catch block executed?
a) When the code in the try block doesn't compile
b) When the exception specified in the catch block is thrown in the try block
c) When the try blok finishes executing
d) When a runtime error occurs
21) Which of the following can you not code in a subclass?
a) a method with the same signature as a method in the superclass
b) a call to a constructor of the superclass
c) a method that's not ddefined by the superclass
d) a call to a private method of the superclass (CORRECT)
22) What propery controls whether the user can edit the text stored in the component?
a) editable
b) focusable
c)mnemonic
d) text (CORRECT)
23) What property to you use to specify a shortcut key that you can use to access the component?
a) editable (CORRECT)
b) focusable
c) mnemonic
d) text
24) To add controls to a form and adjust them so they display correctly, what view do you use?
a) code view
b) design view (CORRECT)
c) output view
d) property view
25) What view do you use to enter the code for a form?
a) Code View
b) Design View
c) Output View
d) Source View
26) ____________ creates a mutually exclusive relationship among radio buttons.
a) ButtonGroup
b) LogicalGroup
c) MutualExclude
d) RadioGroup
27) String components are part of ______________ package?
a) java.swing
b) javax.swing
c) java.awt.swing
d) java.graphics.swing
28) Assuming frame1 refers to JFrame object, what does the following code do?
frame1.setLocationRelativeTo(null);
Aligns the frame in the upper left corner of the screen
Aligns the frame with the upper left corner of the specified component
Centers the frame on the specified component
Centers the frame on the screen
29) What object do you use to use code to modify the items in a list?
A) mutable list
b) list model
c) selector
d) list
30) Which of the following is a low-level event?
a) The user selects an item in a combo box?
b) The user uses a scroll bar on the GUI to scroll down.
c) The user clicks on a button the GUI
d) The user presses the Escape key on the keyboard.
B) PROBLEMS ( 30 points) Clearly write your answers in the provided space.
Clearly write the output of the following statement. (2pts)
Int n = 4;
Int sum = 0;
while (n > 0) {
n = n – 1;
sum = sum + n;
}
System.out.println(“sum = “ + sum);
2)Clearly write the output of the following statement (2 points)
Int sum = 0;
for (int n=1; n < 5; n=n+2) {
sum = sum + n;
}
System.out.println(“sum = “ + sum);
Assume a public method generateOutput is an abstract method in class Product, and this method returns a String value and has a parameter in the type of int.
3.1) Declare this abstract method in the following. Don’t need to write code to define the whole Product class. (2Points)
3.2) Does this Product class have to be defined as abstract? (YES/NO)
yes
3.3) Can you create a child class of this product class by extending it? (YES/NO)
yes
3.4) Can you instantiate an object from this Product class? (YES/NO)
no
3.5) Does every method in this Product class have to be defined as abstract? (YES/NO)
no
Given the following exception hierarchy and code example 1. (4PTS)
Java.lang.Exception
Java.io.IOException
java.io.FileNotFoundException
try
{
// code that throws FileNotFounException or IOException
Statements 1 go here
}
catch (FileNotFoundException e)
{
// code that handles FileNoteFoundException
Statements 2 go here
}
finally
{
Statements 3 go here
}
Statements 4 go here
4.1) If FileNotFoundException is thrown in the try block, will statements 2 in the catch block be executed? (YES/NO)
yes
4.2) If FileNotFoundException is thrown in the try block, will statements 3 in the finally block be executed? (YES/NO)
yes
4.3) If IOException is thrown in the try block, will statements 3 in the finally block be executed? (YES/NO)
yes
4.4) If IOException is thrown in the try block, will statements 4 be executed? (YES/NO_
no
In the following program Test
Public class Test {
Public static void main (String args[])
{
Int n[] = {-1, 2} ;
Int sum = n[0] + n[1];
changeValue(n, sum);
System.out.println(“n[0] = “ + n[0]);
System.out.println(“n[1] = “ + n[1]);
System.out.println(“sum = “ + sum);
} //end of main
Public static void changeValue (int k[], int sum)
{
for (int i = 0; i < k.length; i++)
{
k[i] = k[i] + 1;
sum = sum + k[i];
}
}
} // end of Test
5.1) Whether the method changeValue must be defined as static, and why? (1 Point)
5.2) If the program executed, what is the output of the program? (3 point)
Complete the following Car class according to requirements.
Public class Car {
int year;
String model;
double speed;
//define a constructor which accepts a car’s year and model as arguments and use them to initialize the values for the year and maker attributes. (2 points)
//define a public method, getSpeed, that returns a double value for the instance variable, speed
// and this method does not have an argument. (1 point)
//define public method, setSpeed; that has double type argument (1 point)
//define a public method, goBreak, which does not have any argument and not return anything.
// When this method is called, if the instance variable, speed, is greater than 10, it reduces the speed
// by 10; otherwise, it sets the speed to zero. (2 Pts)
} // end of Car
7)Given the following class diagram, write statements to answer the following questions. (6 points)
7.1) create a MotorVehicle object named mv
MotorVehicle mv = new MotorVehicle();
7.2) for this mv object, assign 4 as number of wheels
7.3) what object oriented concept is demonstrated in the solution for the above question?
7.4) toString() is defined as regular method in both Vehicle and MotorVehicle, what is this phenomenon (of defining the method in both child and parent classes) called?
7.5)
Vehicle v = new MotorVehicle();
System.out.println(v.toString());
In the above statement, toString() defined in which class is called?
7.6) what object-oriented concept the following statement illustrates?
Vehicle v = new MotorVehicle();
Explanation / Answer
12.
Where an object is passed as an argument for a method a reference to the object is actually passed.
Option b is correct choice.
14.
Statement :
All interface can contain static constraints.
Option b is correct choice.
16.
close() method transmits any remaining data from the buffer without closing the stream.
Option a is correct choice.
17.
x is an integer number what is the highest index value in the array is x-1.
Option b is correct choice.
20.
The code within a catch block executed when a runtime error occurs..
Option d is correct choice.
21.
Code View do you use to enter the code for a form.
Option a is correct choice.
30.
Low-level event is user presses the Escape key on the keyboard.
Option d is correct choice.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.