COP3330
Overriding a method differs from overloading a method because:
overridden methods have the same signature
Superclass methods with this level of access cannot be called from subclasses.
private
In producer/consumer relationship, the ----- portion of an application generates data and stores it in a shared object, and the ----- portion of an application reads data from the shared object.
producer, consumer
Which of the following could be used to declare abstract method method1 in abstract class Class1 (method1 returns an int and takes no arguments)?
public abstract int method1()
Which statement correctly passes the array items to method takeArray? Array items contain 10 elements.
takeArray(items)
Which statement correctly passes the array items to method takeArray? Arrayitems contain 10 elements.
takeArray(items).
When an object in concatenated with a String, -----
the Object's toString method is implicitly called to obtain the String representation of the object
The length of a string can be determined by
the String method length()
Class Number is ----- of both Integer and Double
the superclass
Which statement is false?
The methods for primitive types correspond to the methods for the corresponding type-wrapper classes.
Which of the following is false?
The size of an ArrayList can be determined via its length instance variable.
Consider the code segment below. Which of the following statement is false? int[] g; g = new int[23];
The value of g[3] is -1.
Which is a correct static method call of Math class method sqrt?
Math.sqrt(900)
Which of the following class members should usually be private?
Variables (or fields)
Which of the following statements is false?
When a program is executed, array element indices are checked for validity- all indices must be greater than 0 and less than or equal to the length of the array.
Which of the following statement is false?
When you enhance an existing interface with default methods-any class that implemented the original interface will break.
Which of the following statement is true?
You can have many catch blocks to handle different types of exceptions.
Which of the following statements is false?
You cannot prevent client code from creating objects of a class.
You have installed JDK in your computer. However, when you write a command "javacTestClass.java" to complie the file, it shows an error: "javac is not recognized as an intermal or external command". What could be the most common reason?
You have not set the path environment variable properly
Which of the following is the escape character:
\
What will be output after the following Java statements have been executed (assume all variables are of type Int)?
a < b c != d
If the desired Object is not found, binarySearch returns __________.
a negative value
Which keyword is used to specify that a class will define the methods of an interface?
implements
A new class of objects can be created conveniently by -----; the new class (called the -----) starts with the characteristics of an existing class (called the ----), possibly customizing them and adding unique characteristics of its own.
inheritance, subclass, superclass
A new class of objects can be created conveniently by-----; the new class (called the ----) starts with the characteristics of an existing class (called the ----). possibly customizing them and adding unique characteristics of its own.
inheritance, subclass, superclass
In an expression containing values of the types int and double, the ----- values are ----- to ----- values for the uses in the expression.
int, promoted, double
Which statement below initializes array items to contain 3 rows and 2 columns.
int[][] items = {{2, 4}, {6, 8}, {10, 12}};
The class extension on a file means that the file ----.
is produced by the Java compiler (javac)
Inheritance is also known as the -----
is-a relationship.
Which expression adds 1 to the element of array arrayName at index i?
++arrayName[i]
How many Book objects are created by the following statement? Book[] books = new Book[10];
0
What is the result value of c at the end of the following code segment? int c = 8; c++; ++c; c %= 5;
0
Consider array items, which contains the values 0, 2, 4, 6 and 8. If method changeArray is called with changeArray( items, items[ 2 ] ), what values are stored in items after the method has finished executing? public static void ChangeArray( int[] passedArray, int value ) { passedArray[ value ] = 12; value = 5; } // end method ChangeArray
0, 2, 4, 6, 12
What is the value of result after the following Java statement execute (assume all variables are of type int)? a =4; b = 12; c = 37; d = 51; result = d % a * c + a % b + a
119
what would be the output for the following code segment? int a = 1; System.out.print(a++); System.out.print(++a); System.out.print(a);
133
Consider the classes below. What would be the output? class P1{ int a; public P1{ a = 7; } public int callMe(){ return a*2; } } class C1 extends P1{ int b; public C1(){ b = 8; } public int classMe(){ return b*2; } } public class Test{ public static void main(String[] args){ P1 p = new C1(); System.out.println(p.callMe()); } }
16
Given the following declaration: StringBuilder buf = new StringBuilder(); What is the capacity of buf?
16
Which statement creates a random value from the sequence 2, 5, 8, 11 and 14? Suppose randomNumbers is a SecureRandom object.
2 + 3 * randomNumber.netInt(5)
What is the size in bits of an int?
32
For the array that was the correct answer in the previous question, what is the value returned by items[1][0]?
6
Consider the array: s[ 0 ] = 7 s[ 1 ] = 0 s[ 2 ] = -12 s[ 3 ] = 9 s[ 4 ] = 10 s[ 5 ] = 3 s[ 6 ] = 6 The value of s[ s[ 6 ] - s[ 5 ] ] is:
9
Each of the following is a relational or equality operator except -----.
=!
Which statement is false?
A List cannot contain duplicate elements
In a class containing methods with the same name, the methods are distinguished by:
A and B
Which of the following is not possible?
A class that inherits from two classes.
When you pass an array or an individual array element of a reference type to a method, the called method receives -----. When you pass an individual element of a primitive type, the called method receives -----.
A copy of the element's reference, a copy of the element's value
Consider the classes below, declared in the same file: class A { int a; public A() { a = 7; } } class B extends A { int b; public B() { b = 8; } } Which of the statements below is false? a. Both variables a and b are instance variables. b. After the constructor for class B executes, the variable a will have the value 7. c. After the constructor for class B executes, the variable b will have the value 8. d. A reference of type A can be treated as a reference of type B.
A reference of type A can be treated as a reference of type B.
A Java class can have which of the following methods? A. void foo(int a) A. void foo(int a, int b) C. void foo(double a) D. void foo(double a, double b) E void foo(int b)
A, B, C, D
Which of the following statements about arrays are true? A. An array is a group of variables containing values that all have the same type. B. Elements are located by index. C. The length of an array c is deteremined by the expression .length() D. The zeroth element of array c is specified by c[0].
A,B,D
When must a program explicitly use the this reference?
Accessing an instance variable that is shadowed by a local variable
Which of the following methods are overloaded with respect to one another? public int max (int a, int b) {...} public double max (double a, double b) {...} public int max(int a, int b, int c) {...} public double max (double a, double b, double c) {...}
All four methods are overloaded
Which of the following statements is true?
All of the above statements are true.
For the code segment below: switch( q ) { case 1: System.out.println( "apple" ); break; case 2: System.out.println( "orange" ); break; case 3: System.out.println( "banana" ); break; case 4: System.out.println( "pear" ); case 5: System.out.println( "grapes" ); default: System.out.println( "kiwi" ); } // end switch Which of the following values for q will result in kiwi being included in the output?
All of them
Which of the following statements is false?
An interface's private static methods can be called by a default method in the interface
Attempting to access an array element outside of the bounds of an array, causes a(n)
ArrayIndexOutOfBoundsException
Class ---- represents a dynamically resizable array-like data structure.
ArrayList
Which of the following statements is false?
Attributes are specified by the class's methods.
Assume class Book has been declared. Which set of statements creates an array of Books?
Book[] books; books = new Book[numberElements];
Consider the following two Java code segments: Which of the following statements are true?
Both A and B are true.
Consider the abstract superclass below:
Both A and B.
Which statement is false?
Class ArrayList is a fixed-size array.
Which of the following statements do? double[] array; array = new double[14]
Create a double array containing 14 elements.
Consider the following piece of code and choose the output:
Derived fun() called
Which statement is true?
Dividing two integers results in integer division With integer divison, any fractional part of the calculation is lost With integer divison, any fractional part of the calculated is truncated
Which of the following statements is false?
Each class can be used only once to build many objects.
Which edition of Java is geared toward developing large-scale, distributed networking applications and web-based applications?
Enterprise Edition.
It's recommended that you do not explicitly create and use Thread objects to implement concurrency, but rather use the ------ interface
Executor
Assume array items contains the integer values 0, 2, 4, 6 and 8. Which of the following set of statements uses the enhanced for loop to display each value in array items?
For (int i : items){ System.out.printf("%d%n", i); }
You want to store the number of traffic violations against the driving license numbers. The driving license numbers are unique. For each driving license, the number of violations are need to be stored. What data structure should you use in this kind of scenario to add a new record or lookup the number of violations for a given driving license number?
HashMap
Which is the output of the following statements? System.out.printf("Hello "); System.out.println("World");
Hello World
Which of the following statements is false?
If a method does not return a value, the return-value-type in the method declaration can be omitted
Which of the following is false?
If you have an ArrayList variable al, you can get the size of the al by al.Length
Which of the following tasks cannot be performed using an enhanced for loop?
Incrementing the value stored in each element of the array/
class newthread implements Runnable { Consider the above class. If you write the following statement in your main method, what would be the output? NewThread ob1 = new NewThread("One");
Inside run method
Which class is not a type-wrapper class?
Int
Which of the following performs an unboxing conversion? Assume x refers to an Integer object.
Int y = x;
Which method call converts the value in variable stringVariable to an integer?
Integer.parseInt(stringVariable)
What would be the output for the following block of code? System.out.println("i = " + i + " sum =" + sum);
It will be a compiler error in the System.out.println line
What would be the output of the following code segment? 1 for(int i = 0; i < 3; i++) 2 System.out.println( i + " "); 3System.out.println(i);
It will be a compiling error for line 3
Let's say a Book class has a public instance variable called pages. What will be the output of the following piece of code? Book[0].pages = 98; System.out.println(books[0].pages);
It will throw a null pointer exception
You have an Arr with the values {1, 2, 3, 4}. What would be the output of the following line? System.out.println(Arr[4]);
It will throw an exception
A(n)----- allows a program to walk through the collection and remove elements from the collection.
Iterator
Consider classes A, B, and C, where A is an abstract superclass, B is a concrete class that inherits from A and C is a concrete class that inherits from B. Class A declares abstract method originalMethod, implemented in class B. Which of the following statements is true of class C?
None of them.
You have created a generic method with the following syntax. Which of the following option is not true?
One should put <T> instead of using <E> in the above code
Which of the following represents the output of the following code segments?
Print "Base Constructor Called" then print "Derived Constructor Called"
Consider the class below: public class Test { public static void Main( String[] args ) { int[] a = {99, 22, 11, 3, 11, 55, 44, 88, 2, -3}; int result = 0; for ( int i = 0; i < a.Length; ++i ) { if ( a[ i ] > 30 ) result += a[ i ]; } // end for Console.WriteLine( "Result is: {0}", result ); } // end Main } // end class Test The output of this C# program will be:
Result is: 286
Considered you have gathered a list of skills from many of your friends. You put them in an array A = {"java", "c#". "c", "java", "c", "python", "c#", "c"}. Now you want to know the unique list of skills from this list. What data structure would you use to get the unique list of skills in this array?
Set
Which of the following is not a primitive type?
String
Consider the statements below:
String r1 = a.concat(b.concat(c));
Suppose variable gender is MALE and age equals 60, how is the expression ( gender == FEMALE ) && ( age >= 65 ) evaluated?
The condition (gender == FEMALE) is evaluated first and the evaluation stops immediately
A class that implements an interface but does not declare all of the interface's methods must be declared
abstract
A static method can
be called using the class name and a dot(.), call only other static methods of the same class directly, manipulate only static fields in the same class directly
Consider the following piece of code StringBuilder buffer = new StrubgBuilder(5); buffer.append("All your unhappiness hurts me. Isn't it sufficient to prove that I love you all?"); Which of the following is true about the buffer variable after executing the above two lines of code?
buffer will contain the full string appended in the line buffer.append();
String objects are immutable. This means they
cannot be changed
Map method ________ is used to determine whether a map contains a mapping for the specified key.
containsKey
Iterator method ________ determines whether the Collection contains more elements.
hasNext
What will be the output of the following code segment?
hello
For String c = "Hello. She sells sea shells"; The Java statements int i = c.indexOf( "ll" ); int j = c.lastIndexOf( "ll" ); will result in:
i = 2 and j = -1
Another problem related to indefinite postponement is called -----. This occurs when a waiting thread (lets call this thread1) cannot proceed because because it's waiting (either direct or indirectly) for another thread (let's call this thread2) to proceed, while simultaneously thread2 cannot proceed because it's waiting (either directly or indirectly) for thread1 to proceed. The two threads are waiting for each other, so the actions that would enable each thread to continue execution can never occur.
deadlock
Floating-point literals are of type ----- by default.
double
Which of the following promotions of primitive types in not allowed to occur?
double to float
Which of the following promotions of primitive types is not allowed to occur?
double to float
An argument type followed by a(n) ________ in a method's parameter list indicates that the method receives a variable number of arguments of that particular type.
ellipsis(...)
Class Arrays provides method __________ for comparing arrays to determine whether they have the same contents.
equal
A(n) ----- indicates a problem that occurs while a program executes
exception
A(n) ----- indicates a problem that occurs while a program executes.
exception
Polymorphism allows for specifics to be dealt with during
execution
Any field declared with keyword ________ is constant.
final
Which of the following lines is false about final Classes and final methods:
final methods are static
Every object in Java knows its own class and can access this information through method:
getClass
Which of the following represents the output of the following code segment?
it will show an error for line "Derived d = new Base()".
In array items, which expression below accesses the value at row 3 and column 4?
items[3][4]
The command ---- executes a Java application.
java
The classes and interfaces which comprise the collections framework are members of package ________.
java.util
Which command complies the Java source code file Welcome.java?
javac Welcome.java
Which of the following is the superclass constructor call syntax?
keyword super, followed by a set of parentheses containing the superclass constructor arguments.
the main method executes in the ----- thread of execution
main
Which of the following is not a valid Java identifier?
my Value
A new thread begins its life cycle by transitioning to the ---- state.
new
Which of the following is a Scanner method for inputting an integer value?
nextInt
Which of the followings cannot be a constructor of Bike class?(Choose all that applies)
public double Bike(double a, double b) public int Bike()
Types in Java are divided into two categories. The primitive types are boolean, byte, char, short, int, long, float and double. All other types are ________ types.
reference
The statement s1.equalsIgnoreCase(s4) is equivalent to which of the following?
s1.regionMatches(true, s4, 0, s4.length());
When a higher-priority thread enters the ready state, the operating system generally preempts the current running thread (an operation known as preemptive scheduling). Depending on the operating system, a steady influx of higher-priority threads could postpone-possibly indefinitely-the execution of lower-priority threads. Such indefinite postponement is sometimes referred to more colorfully as
starvation
Which of the following keywords allow a subclass to access a superclass method even when the subclass has overridden the superclass method?
super
An exception object's ----- method returns the exception's error message.
toString
You have an Account object called acc that has a balance variable. You have a multi thread programming that uses the acc object. If multiple thread access this acc object at the same time and deduct the balance variable, there is a risk that the balance variable will become inconsistent. How would you prevent the acc object to be modified by multiple thread at the same time and make it thread safe?
using synchronized block
Consider the following Java statements: int x = 9l double y = 5.3; result = calculateValue(x, y); Which of the following statements is false?
x and y are parameters