Java Final Exam

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

Consider the program below: public class Test { public static void main(String[] args) { int[] a; a = new int[10]; for (int i = 0; i < a.length; i++) a[i] = i + 2; int result = 0; for (int i = 0; i < a.length; i++) result += a[i]; System.out.printf("Result is: %d%n", result); } } The output of this program will be: Select one: a. Result is: 64. b. Result is: 65. c. Result is: 67. d. Result is: 62.

b. Result is: 65.

Which of the following statements is true? Select one: A. Performing a task in a program requires a method. B. A method houses the program statements that actually perform its tasks. C. The method hides its statements from its user, just as the accelerator pedal of a car hides from the driver the mechanisms of making the car go faster. D. All of the above

D. All of the above

Attempting to access an array element outside of the bounds of an array causes which one of the following errors. Select one: A. ArrayElementOutOfBoundsException. B. ArrayException. C. ArrayOutOfBoundsException. D. ArrayIndexOutOfBoundsException.

D. ArrayIndexOutOfBoundsException.

Among these expressions, which is(are) of type String? A. "0" B. "ab" + "cd" C. '0' D. Both (A) and (B) above E. (A), (B) and (C) above.

D. Both (A) and (B) above

Consider the following class definition: public class MyClass { private int value; public void setValue(int i){ /* code */ } // Other methods... } The method setValue assigns the value of i to the instance field value. What could you write for the implementation of setValue? A. value = i; B. this.value = i; C. value == i; D. Both (A) and (B) and above E. (A), (B) and (C) above.

D. Both (A) and (B) and above

Which statement is false? Select one: A. A collection is an object that can hold references to other objects. B. Collections are carefully constructed for rapid execution and efficient use of memory. C. The collection interfaces declare the operations that can be performed on each type of collection. D. Collections discourage software reuse because they are non-portable.

D. Collections discourage software reuse because they are non-portable.

Which of the following statements is true? Select one: A. Information hiding is achieved by restricting access to class members via keyword public. B. None of the above is true. C. The private members of a class are directly accessible to the clients of a class. D. Methods and instance variables can both be either public or Private.

D. Methods and instance variables can both be either public or Private.

Which of the following statements is true? Select one: A. Information hiding is achieved by restricting access to class members via keyword public. B. None of the above is true. C. The private members of a class are directly accessible to the clients of a class. D. Methods and instance variables can both be either public or private.

D. Methods and instance variables can both be either public or private.

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"); } Overriding a method differs from overloading a method because: Select one: A. Overloaded methods have the same signature. B. Both of the above. C. Neither of the above. D. Overridden methods have the same signature.

D. Overridden methods have the same signature.

Overriding a method differs from overloading a method because: Select one: A. Overloaded methods have the same signature. B. Both of the above. C. Neither of the above. D. Overridden methods have the same signature.

D. Overridden methods have the same signature.

Consider the two methods (within the same class) public static int foo(int a, String s) { s = "Yellow"; a=a+2; return a; } public static void bar() { int a=3; String s = "Blue"; a = foo(a,s); System.out.println("a="+a+" s="+s); } public static void main(String args[]) { bar(); } What is the out from the code segment above? A. a = 3 s = Blue B. a = 5 s = Yellow C. a = 3 s = Yellow D. a = 5 s = Blue

D. a = 5 s = Blue

The fields in an interface are implicitly specified as, A. static only B. protected C. private D. both static and final E. none of the above.

D. both static and final

What is the type and value of the following expression? (Notice the integer division) -4 + 1/2 + 2*-3 + 5.0 A. int -5 B. double -4.5 C. int -4 D. double -5.0 E. None of the above.

D. double -5.0

Which syntax imports all static members of class Math? Select one: A. import static java.lang.Math.*; B. None of the above C. import java.lang.Math.*; D. import static java.lang.Math;

D. import static java.lang.Math;

Which of the following statements creates a multidimensional? Array with 3 rows, where the first row contains 1 element, the second row contains 4 elements and the final row contains 2 elements? Select one: A. int[][] items = {{1}, {4}, {2}}; B. int[][] items = {{1}, {2, 3, 4, 5}, {6, 7}, {}); C. int[][] items = {{1, null, null, null}, {2, 3, 4, 5}, {6, 7, null, null}}; D. int[][] items = {{1}, {2, 3, 4, 5}, {6, 7}};

D. int[][] items = {{1}, {2, 3, 4, 5}, {6, 7}};

Which of the following is the superclass constructor call syntax? Select one: A. keyword super, followed by a dot (.) B. None of the above C. keyword super, followed by a dot and the superclass constructor name D. keyword super, followed by a set of parentheses containing the superclass constructor arguments

D. keyword super, followed by a set of parentheses containing the superclass constructor arguments

Which of the following is the superclass constructor call syntax? Select one: A. keyword super, followed by a dot (.) B. None of the above C. keyword super, followed by a dot and the superclass constructor name D. keyword super, followed by a set of parentheses containing the superclass constructor arguments

D. keyword super, followed by a set of parentheses containing the superclass constructor arguments

Set methods are also commonly called ________ methods and get methods are also commonly called ________ methods. Select one: A. query, accessor B. query, mutator C. accessor, mutator D. mutator, accessor

D. mutator, accessor

A constructor A. Must have the same name as the class it is declared within. B. Is used to create objects. C. May be declared private D. Both (A) and (B) above E. (a), (b) and (c) above.

E. (a), (b) and (c) above.

Which of the following variable declarations would NOT compile in a java program? A. int var; B. int VAR; C. int var1; D. int var_1; E. int 1_var;.

E. int 1_var;.

Which of the following is NOT a valid Java identifier? Select one: A. my Value B. $_AAA1 C. width D. m_x

A. my Value

Multiple inheritance means, (Select TWO) A. one class inheriting from more super classes B. more classes inheriting from one super class C. more classes inheriting from more super classes D. None of the above E. (a) and (b) above.

A. one class inheriting from more super classes B. more classes inheriting from one super class

Which superclass members are inherited by all subclasses of that superclass? Select one: A. protected instance variables and methods. B. protected constructors. C. private instance variables and methods. D. private constructors.

A. protected instance variables and methods.

Which JFileChooser method specifies what users can select in the dialog? Select one: A. setFileSelectionMode B. setSelectionMode C. showOpenDialog D. None of these

A. setFileSelectionMode

Consider the code segment below. Which of the following statements is FALSE? int[] g; g = new int[23]; Select one: A. The second statement creates the array. B. The value of g[3] is -1. C. The first statement declares an array reference. D. g is a reference to an array of integers.

B. The value of g[3] is -1.

What is output by the following Java code segment? int temp = 180; while (temp != 80) { if (temp > 90) { System.out.print("This porridge is too hot! "); // cool down temp = temp - (temp > 150 ? 100 : 20); } else { if (temp < 70) { System.out.print( "This porridge is too cold! "); // warm up temp = temp + (temp < 50 ? 30 : 20); } } } if (temp == 80) System.out.println("This porridge is just right!"); Select one: A. This porridge is too cold! This porridge is just right! B. This porridge is too hot! This porridge is just right! C. This porridge is just right! D. None of the above.

B. This porridge is too hot! This porridge is just right!

All import declarations must be placed ________ Select one: A. inside the class declaration's body. B. before the class declaration. C. after the class declaration. D. all of the above will work.

B. before the class declaration.

String objects are immutable. This means they ____________. Select one: A. must be initialized B. cannot be changed C. none of the above D. cannot be deleted

B. cannot be changed

Every class in Java, except ________, extends an existing class. Select one: A. Class B. Integer C. Object D. String

C. Object

Which of the following is false? Select one: A. Memory leaks using Java are rare because of automatic garbage collection. B. Method finalize does not take parameters and has return type void. C. Objects are marked for garbage collection by method finalize. D. The garbage collector reclaims unused memory.

C. Objects are marked for garbage collection by method finalize.

Which statement is not true in java language? A. A public member of a class can be accessed in all the packages. B. The methods of the same class cannot access a private member of a class. C. A private member of a class cannot be accessed from its derived class. D. A protected member of a class can be accessed from its derived class.

B. The methods of the same class cannot access a private member of a class.

Which of the following creates the string of the numbers from 1 to 1000 most efficiently? Select one: A. StringBuilder sb = new StringBuilder(10); for (int i = 1; i <= 1000; i++) sb.append(i); String s = new String(sb); B. String s; for (int i = 1; i <= 1000; i++) s += i; C. StringBuilder sb = new StringBuilder(3000); for (int i = 1; i <= 1000; i++) sb.append(i); String s = new String(sb); D. All are equivalently efficient.

C. StringBuilder sb = new StringBuilder(3000); for (int i = 1; i <= 1000; i++) sb.append(i); String s = new String(sb);

The default value of a static integer variable of a class in Java is, A. 0 B. 1 C. Garbage value D. Null

A. 0

What is the value of result after the following Java statements execute (assume all variables are of type int)? a = 4; b = 12; c = 37; d = 51; result = d % a * c + a % b + a; Select one: A. 119 B. 51 C. 127 D. 59

A. 119

Each repetition of a loop is known as what? Select one: A. An iteration B. A cycle C. An execution D. A Lap

A. An iteration

If you are using a block of statements, don't forget to enclose all of the statements in a set of: A. Braces B. Double quotes C. Semicolons D. Parentheses

A. Braces

Which statement is false? Select one: A. Classes are reusable software components. B. A class is to an object as a blueprint is to a house. C. Performing a task in a program requires a method. D. A class is an instance of its object

A. Classes are reusable software components.

Which of the following statements is false? Select one: A. Each class can be used only once to build many objects. B. Reuse helps you build more reliable and effective systems because existing classes and components often have undergone extensive testing, debugging, and performance tuning. C. Just as the notion of interchangeable parts was crucial to the Industrial Revolution, reusable classes are crucial to the software The revolution that has been spurred by object technology. D. Avoid reinventing the wheel—use existing high-quality pieces wherever possible. This software reuse is a key benefit of object-oriented programming.

A. Each class can be used only once to build many objects.

Which of the following classes is not used for file input? Select one: A. Formatter B. FileInputStream C. ObjectInputStream D. FileReader

A. Formatter

Which of the following tasks cannot be performed using an enhanced for loop? Select one: A. Incrementing the value stored in each element of the array. B. Displaying all even element values in an array. C. Calculating the product of all the values in an array. D. Comparing the elements in an array to a specific value.

A. Incrementing the value stored in each element of the array.

Consider the following code, public class MyClass { public MyClass(){/*code*/} // more code... } To instantiate MyClass, you would write? A. MyClass mc = new MyClass(); B. MyClass mc = MyClass(); C. MyClass mc = MyClass; D. MyClass mc = new MyClass;

A. MyClass mc = new MyClass();

Consider the statements below: String a = "JAVA: "; String b = "How to "; String c = "Program"; Which of the statements below will create the String r1 = "JAVA: How to Program"? Select one: A. String r1 = a.concat(b.concat(c)); B. String r1 = c.concat(c.concat(b)); C. String r1 = b.concat(c.concat(a)); D. String r1 = c.concat(b.concat(a));

A. String r1 = a.concat(b.concat(c));

In the catch block below, what is e? catch (ArithmeticException e) { System.err.printf(e); } Select one: A. The name of catch block's exception parameter. B. A finally block. C. The type of the exception being caught. D. An exception handler.

A. The name of catch block's exception parameter.

A program must explicitly use the this reference when accessing an instance variable that is shadowed by a local variable. Select one: A. True B. False

A. True

All objects of a class share static class variables. Select one: A. True B. False

A. True

The Java statement: Integer x = 7; performs an auto-boxing conversion. Select one: A. True B. False

A. True

The regular expressions "A*" and "A+" will both match "AAA", but only "A*" will match an empty string. Select one: A. True B. False

A. True

What will be output after the following Java statements have been executed (assume all variables are of type int)? a = 4; b = 12; c = 37; d = 51; if ( a < b ) System.out.println( "a < b" ); if ( a > b ) System.out.println( "a > b" ); if ( d <= c ) System.out.println( "d <= c" ); if ( c != d ) System.out.println( "c != d" ); Select one: A. a < b c != d B. a < b d <= c c != d C. a > b c != d D. a < b c < d a != b

A. a < b c != d

StringBuilder objects can be used in place of String objects if ________. Select one: A. all of these B. the string data is not constant C. the string data size may grow D. the programs frequently perform string concatenation

A. all of these

Which JFileChooser method returns the file the user selected? Select one: A. getSelectedFile B. getOpenDialog C. showOpenDialog D. getFile

A. getSelectedFile

A String constructor cannot be passed ____________. Select one: A. int arrays B. byte arrays C. char arrays D. Strings

A. int arrays

Which of the following is not a package in the Java API? Select one: A. java.component B. java.awt C. javax.swing.event D. java.lang

A. java.component

Analyze the following code: public class Test { public static void main(String[] args) { System.out.println(max(1, 2)); } public static double max(int num1, double num2) { System.out.println("max(int, double) is invoked"); if (num1 > num2) return num1; else return num2; } public static double max(double num1, int num2) { System.out.println("max(double, int) is invoked"); if (num1 > num2) return num1; else return num2; } } A. The program cannot compile because you cannot have the print statement in a non-void method B. The program cannot compile because the compiler cannot determine which max method should be invoked C. The program runs and prints 2 followed by "max(int, double)" is invoked D. The program runs and prints 2 followed by "max(double, int)" is invoked E. The program runs and prints "max(int, double) is invoked" followed by 2.

B. The program cannot compile because the compiler cannot determine which max method should be invoked

What will be the value of x after the following code is executed? int x = 10; while (x < 100) { x += 10; } Select one: A. 90 B. 100 C. 110 D. This is an infinite loop.

B. 100

Which of the following values for q will result in kiwi being included in the output? Select one: A. 2 B. Any integer less than 1 and greater than or equal to 4 C. 1 D. 3

B. Any integer less than 1 and greater than or equal to 4

Which of the following statements is true? Select one: A. If it is necessary to read a sequential file again, the program can simply keep reading—when the end of the file is reached, the Scanner is automatically set to point back to the beginning of the file. B. Class Scanner does not provide the ability to reposition to the beginning of the file. C. Class Scanner provides the ability to reposition to the beginning of a file with method seek. D. Class Scanner provides the ability to reposition to the beginning of a file with method reposition.

B. Class Scanner does not provide the ability to reposition to the beginning of the file.

Which of the following statements is true? Select one: A. Constructors can specify parameters and return types. B. Constructors can specify parameters but not return types. C. Constructors cannot specify parameters but can specify return types. D. Constructors can specify neither parameters nor return types.

B. Constructors can specify parameters but not return types.

A constructor can specify return types and return values. Select one: A. True B. False

B. False

An anonymous String has no value. Select one: A. True B. False

B. False

Assuming x refers to an Integer object, the Java statement: Integer y = x; performs an unboxing conversion. Select one: A. True B. False

B. False

This variable controls the number of times that the loop iterates. Select one: A. Counter variable B. Loop control variable C. Running total D. Decrement variable

B. Loop control variable

Which set of statements totals the items in each row of two-dimensional array items, and displays each row's total? Select one: A. int total = 0; for (int row = 0; row < items.length; row++) { for (int column = 0; column < items[column].length; column++) total += items[row][column]; System.out.printf("Row %d's total is %d%n", row, total); } B. for (int row = 0; row < items.length; row++) { int total = 0; for (int column = 0; column < items[row].length; column++) total += items[row][column]; System.out.printf("Row %d's total is %d%n", row, total); } C. int total = 0; for (int row = 0; row < items.length; row++) { for (int column = 0; column < items[row].length; column++) total += items[row][column]; System.out.printf("Row %d's total is %d%n", row, total); } D. for (int row = 0; row < items.length; row++) { int total = 0; for (int column = 0; column < items[column].length; column++) total += items[row][column]; System.out.printf("Row %d's total is %d%n", row, total); }

B. for (int row = 0; row < items.length; row++) { int total = 0; for (int column = 0; column < items[row].length; column++) total += items[row][column]; System.out.printf("Row %d's total is %d%n", row, total); }

Consider the code segment below. if (gender == 1) { if (age >= 65) ++seniorFemales; } This segment is equivalent to which of the following? Select one: A. if (gender == 1 || age >= 65) ++seniorFemales; B. if (gender == 1 && age >= 65) ++seniorFemales; C. if (gender == 1 AND age >= 65) ++seniorFemales; D. if (gender == 1 OR age >= 65) ++seniorFemales;

B. if (gender == 1 && age >= 65) ++seniorFemales;

This type of loop will always be executed at least once. Select one: A. pre-test loop B. post-test loop C. sentinel loop D. for loop

B. post-test loop

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. Select one: A. static B. reference C. declared D. source

B. reference

________ is a graphical language that allows people who design software systems to use an industry standard notation to represent them. Select one: A. The Unified Graphical Language B. The Unified Design Language C. The Unified Modeling Language D. None of the above

C. The Unified Modeling Language

Which of the following statements is true? Select one: A. The capacity of a StringBuilder is equal to its length. B. The capacity of a StringBuilder cannot exceed its length. C. The length of a StringBuilder cannot exceed its capacity. D. Both a and b are true

C. The length of a StringBuilder cannot exceed its capacity.

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: ________ Select one: A. 10 B. 3 C. 9 D. 0

C. 9

Which of the following is not possible? Select one: A. All of the above are possible. B. A class that implements two interfaces C. A class that inherits from two classes. D. A class that inherits from one class and implements an interface.

C. A class that inherits from two classes.

Which of the following is true? A. A finally block is executed before the catch block but after the try block. B. A finally block is executed, only after the catch block is executed. C. A finally block is executed whether an exception is thrown or not. D. A finally block is executed, only if an exception occurs. E. None of the above.

C. A finally block is executed whether an exception is thrown or not.

Which statement best describes the relationship between superclass and subclass types? Select one: A. A superclass reference can be assigned to a subclass variable, but a subclass reference cannot be assigned to a superclass variable. B. A subclass reference cannot be assigned to a superclass variable and a superclass reference cannot be assigned to a subclass variable. C. A subclass reference can be assigned to a superclass variable, but a superclass reference cannot be assigned to a subclass variable. D. A subclass reference can be assigned to a superclass variable and a superclass reference can be assigned to a subclass variable.

C. A subclass reference can be assigned to a superclass variable, but a superclass reference cannot be assigned to a subclass variable.

Which one of the following is not true? A. A class containing abstract methods is called an abstract class. B. Abstract methods should be implemented in the derived class. C. An abstract class cannot have non-abstract methods. D. A class must be qualified as 'abstract' class, if it contains one abstract method. E. None of the above.

C. An abstract class cannot have non-abstract methods.

Which of the following is not true? A. An interface can extend another interface. B. A class which is implementing an interface must implement all the methods of the interface. C. An interface can implement another interface. D. An interface is a solution for multiple inheritance in java. E. None of the above.

C. An interface can implement another interface.

Which of the following statements is false? Select one: A. All of the options are true. B. Exception handling enables programmers to write robust and fault-tolerant programs. C. Exception handling can catch but not resolve exceptions. D. Exception handling can resolve exceptions.

C. Exception handling can catch but not resolve exceptions.

Analyze the following code: public class Test { public static void main(String[] args) { int[] x = new int[5]; int i; for (i = 0; i < x.length; i++) x[i] = i; System.out.println(x[i]); } } A. The program displays 0 1 2 3 4 B. The program displays 4 C. The program has a runtime error because the last statement in the main method causes an ArrayIndexOutOfBoundsException D. The program has syntax error because i is not defined in the last statement in the main method E. The program displays 1 2 3 4 5.

C. The program has a runtime error because the last statement in the main method causes an ArrayIndexOutOfBoundsException

Which type of class cannot be instantiated. Select one: A. final B. polymorphic C. abstract D. concret

C. abstract

Which of the following code segments does not increment val by 3? Select one: A. val += 3; B. val = val + 1; val = val + 1; val = val + 1; C. c = 3; val = val + (c == 3 ? 2 : 3); D. All of the above increment val by

C. c = 3; val = val + (c == 3 ? 2 : 3);

To prevent any method from overriding, we declare the method as, A. static B. const C. final D. abstract E. none of the above.

C. final

If the superclass contains only abstract method declarations, the superclass is used for ________. Select one: A. Neither B. implementation inheritance C. interface inheritance D. Both

C. interface inheritance

Declaring a method final means: Select one: A. it will prepare the object for garbage collection. B. it cannot be overloaded. C. it cannot be overridden. D. it cannot be accessed from outside its class.

C. it cannot be overridden.

Which of the following could be used to declare abstract method method1 in abstract class Class1 (method1 returns an int and takes no arguments)? Select one: A. public int abstract method1(); B. public int nonfinal method1(); C. public abstract int method1(); D. public int method1();

C. public abstract int method1();

Which of the following is not a Java primitive type? Select one: A. char B. byte C. real D. double

C. real

A key part of enabling the JVM to locate and call method main to begin the app's execution is the ________ keyword, which indicates that main can be called without first creating an object of the class in which the method is declared. Select one: A. stable B. private C. static D. public

C. static

To catch an exception, the code that might throw the exception must be enclosed in a ________. Select one: A. finally block B. catch block C. try block D. throws block

C. try block

Consider array items, which contains the values 0, 2, 4, 6, and 8. If method changeArray is called with the method call 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; } Select one: A. 0, 2, 5, 6, 12 B. 0, 2, 12, 6, 8 C. 0, 2, 4, 6, 5. D. 0, 2, 4, 6, 12.

D. 0, 2, 4, 6, 12.

Which of the following is TRUE? A. In java, an instance field declared public generates a compilation error. B. int is the name of a class available in the package java.lang C. Instance variable names may only contain letters and digits. D. A class always has a constructor (even if automatically supplied by the java compiler). E. The more comments in a program, the faster the program runs.

D. A class always has a constructor (even if automatically supplied by the java compiler).


Ensembles d'études connexes

Chapter 4 Study Guide (From Quiz)

View Set

Chapter 8 Beginning - The Impact of the French Revolution

View Set

Algebra 2.11: Reasoning + quiz answers

View Set

Chapter 7: Identity and Difference in Organizational Life

View Set