Programming 2150 ggc
Every instance data field f in the class can be referenced using this.f in an instance method the same class.
true
All the update methods in Vector are synchronized.
) true
The UML uses ________ before a member name to indicate that the member is public.
1) +
Object oriented programming approaches are often used in industry because:
1) multiple inheritance makes programming easier; ) companies ultimately save money when doing good OO ; 4) compaines can integrate new functionality fasr and with less bugs
An immutable class cannot have
1) public data fields
Java uses ________ to reference the current object.
1) this
A static data field can be accessed from any method in the same class.
1) true
Every class has a toString() method and an equals() method.
1) true
If a method is declared private in the superclass, you may declare the method public in the subclass. (Be careful, this may be trickier than it sounds. You may want to build a simple test case in your favorite IDE before answering).
1) true
If an object is an instance of class A, and class A is a subclass of class B, then the object is also an instance of class B.
1) true
The elements in an ArrayList can be traversed using ListIterator or using index.
1) true
The internal state of an immutable class cannot be changed. String is an immutable class.
1) true
Will the following code have a compile error? ArrayList<int> list = new ArrayList<int>();
1) true
You can use the operator == to check whether two variables refer to the same object, and use the equals() method to check the equality of contents of the two objects.
1) true
What will be displayed by invoking xfunction(6)? public static int xfunction(int n){ if(n<=1) return 1; else return n + xfunction(n-2);
13
Consider the following declaration for a class A. class A{ int x=x; int y=y;
2 and 3
The UML uses ________ before a member name to indicate that the member is private.
2) -
When the JVM walks back through the object hierarchy to find a matching method implementation at runtime this is called:
2) dynamic binding
ArrayList<Integer> is a subtype of ArrayList<Double>.
2) false
Every instance data field f in the class can be referenced using this.f in a static method the same class.
2) false
Superclasses contain more features than their subclasses.
2) false
You can substitute a generic type with a primitive data type.
2) false
In OOP, you declare a class that extends another class. This is called
2) inheritance
Sometimes, we need to take a more general object reference and adjust it so that we can access the fields and methods of a subclass. This operation is called:
3) casting
In OOP, a reference variable can reference a subtype object. This is called ________.
3) polymorphism
What will be displayed by invoking xfunction(1234)? public static int xfunction(int n){ if(n>0){ System.out.println(n%10+""); xfunction(n/10);
4 3 2 1
Consider the following recursive method. public static int xfunction(int value){ if(value>=0){ return 5*m(value-2); else return 1 What value is returned when invoking m(5)?
4) 125
A constructor can access
A private instance variable 3) A public instance variable
can be accessed from any static method in the class.
A static variable
false
All the update methods in the Java Collection framework are synchronized.
________ can be accessed from any instance method in the class.
An instance variable 3) A static variable
What exception type does the following program throw? public class Test { public static void main(String[] args) { Object o = new Object(); String d = (String)o; } }
class castException
G2:What will be the output of the program? try { int x = 0; int y = 5/x; } catch(Exception e) { System.out.println("Exception"); } catch(ArithmetricException ae) { System.out.println("Arithmetic Exception"); } System.out.println("Finished");
Compilation fails
G1:A class can extend more than one class.
False
G1:If a method in a subclass has the same signature as a method in its superclass with the same return type, is the method overriden or overloaded?
Method overridden
stops
The base case ________ the recursion.
retrieves and removes the head of this queue and throws an exception if this queue is empty
The remove() method in the Queue interface
G1:The superclass constructor always gets executed before the subclass constructor.
True
G2:A try block can be followed by multiple catch blocks
True
[2, 3]
What will be displayed by the following code? import java.util.*; public class Test{ public static void main String[]args) throws Exception{ ArrayList<Integer> list = new ArrayList(); list.add(1); list.add(2); list.add(1); list.add(3); remove(list, 1); Sysout(list);
true
You can sort an array using the sort method in the Arrays class.
true
You can use index to traverse elements in an ArrayList.
What will be displayed by the following code? ArrayList<Integer> list = new ArrayList<Integer>(); list.add(0); list.add(1); list.add(2); list.add(1,4); list.set(2,30); System.out.println(list);
[0, 4, 30, 2]
G2: What will be the output of the program? public class X { public static void main(String [] args) { try { badMethod(); System.out.print("A"); } catch (Exception ex) { System.out.print("B"); } finally { System.out.print("C"); } System.out.print("D"); } public static void badMethod() { throw new Error(); } }
c is printed before existing with an error message
G2:What exception type does the following program throw? public class Test { public static void main(String[] args) { Object o = null; System.out.println(o.toString()); } }
d) NullPointerException
ListIterator is a subclass of Iterator.
false
What will be displayed by invoking xfunction(1234)? public static int xfunction(n){ if(n<=0){ System.out.println(n%10+""); xfunction(n/10);
nothing
Ultimately, every class in java inherits from the superclass named
object
G2:Correct a compile error in the following code: public void m(int value) { if (value < 40) throw new Exception("value is too small");
public void m(int value) throw Exception { if (value < 40) throw new Exception("value is too small"); }
The poll() method in the Queue interface
retrieves and removes the head of this queue, or null if this queue is empty
The peek() method in the Queue interface
retrieves, but does not remove, the head of this queue, returning null if this queue is empty
The element() method in the Queue interface_
retrieves, but does not remove, the head of this queue, throwing an exception if this queue is empty
Will the following code have a compile error? ArrayList<String> list = new ArrayList<String>(); list.add(3);
true
You should use the ArrayList or LinkedList to store duplicate elements.
true