INSY 4305 Final

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

Which formatting flag indicates that the floating-point values should be output with a thousands separator? 1) plus (+). 2) minus (-). 3) comma (,). 4) period (.).

3) comma (,).

21. Which of the following statements is true? a. The throw statement is used to throw an exception. b. The throw statement is used to specify that a method will throw an exception. c. The throw statement is used to access an exception parameter. d. All of the above.

a. The throw statement is used to throw an exception.

38. Which of the following initializer lists would correctly set the elements of array n? a. int[] n = { 1, 2, 3, 4, 5 };. b. array n[ int ] = { 1, 2, 3, 4, 5 };. c. int n[ 5 ] = { 1; 2; 3; 4; 5 };. d. int n = new int( 1, 2, 3, 4, 5 );.

a. int[] n = { 1, 2, 3, 4, 5 };.

44. Which statement below initializes array items to contain 3 rows and 2 columns? a. int[][] items = { { 2, 4 }, { 6, 8 }, { 10, 12 } };. b. int[][] items = { { 2, 6, 10 }, { 4, 8, 12 } };. c. int[][] items = { 2, 4 }, { 6, 8 }, { 10, 12 };. d. int[][] items = { 2, 6, 10 }, { 4, 8, 12 };.

a. int[][] items = { { 2, 4 }, { 6, 8 }, { 10, 12 } };.

9. The statement s1.equalsIgnoreCase(s4) is equivalent to which of the following? a. s1.regionMatches(true, 0, s4, 0, s4.length()); b. s1.regionMatches(0, s4, 0, s4.length()); c. s1.regionMatches(0, s4, 0, s4.length); d. s1.regionMatches(true, s4, 0, s4.length);

a. s1.regionMatches(true, 0, s4, 0, s4.length());

8. The length of a string can be determined by ________. a. the String method length() b. the String instance variable length c. the String method strlen() d. All of the above.

a. the String method length()

7. String objects are immutable. This means they ________.

c. cannot be changed

41. In this question, assume a class, Book, has been defined. Which set of statements creates an array of Book objects? a. Book[] books; books = new Book[ numberElements ]; b. Book[] books]; books = new Book()[ numberElements ]; c. new Book() books[]; books = new Book[ numberElements ]; d. All of the above.

Book[] books; books = new Book[ numberElements ];

39. Which expression adds 1 to the element of array arrayName at index i? a. ++arrayName[ i ]. b. arrayName++[ i ]. c. arrayName[ i++ ]. d. None of the above.

a. ++arrayName[ i ].

56. Q1: What is output by the following Java code segment? int temp; temp = 200; if ( temp > 90 ) System.out.println( "This porridge is too hot." ); if ( temp < 70 ) System.out.println( "This porridge is too cold." ); if ( temp == 80 ) System.out.println( "This porridge is just right!" ); a.This porridge is too hot. b.This porridge is too cold. c.This porridge is just right! d.None of the above

a.This porridge is too hot.

27. Which of the following is false? a. A subclass is often larger than its super class. b. A superclass object is a subclass object. c. The class following the extends keyword in a class declaration is that direct super class of the class being declared. d. Java uses interfaces to provide the benefits of multiple inheritance

b. A superclass object is a subclass object.

16. Which of the following statements is false? a. Exception handling enables programmers to write robust and fault-tolerant programs. b. Exception handling can catch but not resolve exceptions. c. Exception handling can resolve exceptions. d. All of the above are true.

b. Exception handling can catch but not resolve exceptions.

29. Every class in Java, except ________, extends an existing class. a. Integer. b. Object. c. String. d. Class.

b. Object.

30. Overriding a method differs from overloading a method because: a. Overloaded methods have the same signature. b. Overridden methods have the same signature. c. Both of the above. d. Neither of the above.

b. Overridden methods have the same signature.

2. Which of the following statements is false? a. A Path represents the location of a file or directory. b. Path objects open files and provide file-processing capabilities. c. Class Paths is used to get a Path object representing a file or directory location. d. The static method get of class Paths converts a String representing a file's or directory's location into a Path object

b. Path objects open files and provide file-processing capabilities.

18. In the catch block below, what is e? catch (ArithmeticException e) { System.err.printf(e); } a. The type of the exception being caught. b. The name of catch block's exception parameter. c. A finally block. d. An exception handler.

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

13. Given the following declarations: StringBuilder buffer = new StringBuilder("Testing Testing"); buffer.setLength(7); buffer.ensureCapacity(5); Which of the following is true? a. buffer has capacity 5. b. buffer has capacity 31. c. buffer has content "Testin". d. buffer has length 15

b. buffer has capacity 31.

1. Streams that input bytes from and output bytes to files are known as ________. a. bit-based streams b. byte-based streams c. character-based streams d. Unicode-based streams

b. byte-based streams

33. The static method ________ of class String returns a formatted String. a. printf. b. format. c. formatString. d. toFormatString

b. format.

55. Consider the code segment below. if ( gender == 1 ) if ( age >= 65 ) ++seniorFemales; This segment is equivalent to which of the following? 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;

22. If the superclass contains only abstract method declarations, the superclass is used for ________. a. implementation inheritance. b. interface inheritance. c. Both. d. Neither.

b. interface inheritance.

5. Files static method ________ receives a Path and returns a boolean indicating whether that Path represents a directory on disk. a. isDiskDirectory b. isDirectory c. isFileDirectory d. isPath

b. isDirectory

59. Declaring instance variables ________ is known as data hiding or information hiding. a. secure b. private c. static d. masked

b. private

32. Which superclass members are inherited by all subclasses of that superclass? a. private instance variables and methods. b. protected instance variables and methods. c. private constructors. d. protected constructors.

b. protected instance variables and methods.

60. Java are divided into two categories. The primitive types are boolean, byte, char, short, int, long, float and double. All other types are _______ types. a. static b. reference c. declared d. source

b. reference

3. A(n) ________ path starts from the directory in which the application began executing. a. absolute b. relative c. parallel d. comparative

b. relative

42. Which statement correctly passes the array items to method takeArray? Array items contains 10 elements. a. takeArray( items[] ). b. takeArray( items ). c. takeArray( items[ 9 ] ). d. Arrays cannot be passed to methods—each item must be sent to the method separately.

b. takeArray( items ).

45. To declare a method as static, place the keyword static before ________ in the method's declaration. a. the method modifier. b. the return type. c. the method name. d. the argument list.

b. the return type.

47. Consider the following Java statements: int x = 9; double y = 5.3; result = calculateValue( x, y ); Which of the following statements is false? a. A method is called with its name and parentheses. b. x and y are parameters. c. Copies of x and y are passed to the method calculateValue. d. x and y are arguments.

b. x and y are parameters.

12. Given the following declaration: StringBuilder buf = new StringBuilder(); What is the capacity of buf? a. 0 b. 10 c. 16 d. 20

c. 16

6. What does the following statement do? Scanner scanner = new Scanner(Paths.get("test.txt"));

c. Opens a text file for input.

26. A class that implements an interface but does not declare all of the interface's methods must be declared ________. a. public. b. interface. c. abstract. d. final.

c. abstract.

57. Which of the following code segments does not increment val by 3: 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

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

14. To find the character at a certain index position within a String, use the method ________. a. getChars, with the index as an argument b. getCharAt, with the index as an argument c. charAt, with the index as an argument d. charAt, with the character you are searching for as an argument

c. charAt, with the index as an argument

11. For String c = "hello world"; The Java statements int i = c.indexOf('o'); int j = c.lastIndexOf('l'); will result in:

c. i = 4 and j = 9.

40. Consider integer array values, which contains 5 elements. Which statements successfully swap the contents of the array at index 3 and index 4? a. values[ 3 ] = values[ 4 ]; values[ 4 ] = values[ 3 ]; b. values[ 4 ] = values[ 3 ]; values[ 3 ] = values[ 4 ]; c. int temp = values[ 3 ]; values[ 3 ] = values[ 4 ]; values[ 4 ] = temp; d. int temp = values[ 3 ]; values[ 3 ] = values[ 4 ]; values[ 4 ] = values[ 3 ];

c. int temp = values[ 3 ]; values[ 3 ] = values[ 4 ]; values[ 4 ] = temp;

36. Set methods are also commonly called ________ methods and get methods are also commonly called ________ methods. a. query, mutator. b. accessor, mutator. c. mutator, accessor. d. query, accessor.

c. mutator, accessor.

61. Which of the following is a Scanner method for inputting an integer value? a. nextInteger b. integer c. nextInt d. int

c. nextInt

17. To catch an exception, the code that might throw the exception must be enclosed in a ________. a. throws block. b. catch block. c. try block. d. finally block.

c. try block.

46. Declaring main as static allows the JVM to invoke main____. c. without creating an instance of the class in which main is declared

c. without creating an instance of the class in which main is declared

54. Which of the following statements about a do...while repetition statement is true? a.The body of a do...while loop is executed only if the terminating condition is true. b.The body of a do...while loop is executed only once. c.The body of a do...while loop is always executed at least once. d.None of the above

c.The body of a do...while loop is always executed at least once.

50. In a class containing methods with the same name, the methods are distinguished by: a. Number of arguments. b. Types of arguments. c. Return type. d. (a) and (b). e. (b) and (c).

d. (a) and (b).

43. 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; } // end method changeArray 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

49. Which statement creates a random value from the sequence 2, 5, 8, 11 and 14. Suppose randomNumbers is a Random object. a. 2 + 5 * randomNumbers.nextInt( 3 ); b. 3 + 2 * randomNumbers.nextInt( 5 ); c. 5 + 3 * randomNumbers.nextInt( 2 ); d. 2 + 3 * randomNumbers.nextInt( 5 );

d. 2 + 3 * randomNumbers.nextInt( 5 );

31. 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

d. A reference of type A can be treated as a reference of type B

34. When must a program explicitly use the this reference? a. Accessing a private variable. b. Accessing a public variable. c. Accessing a local variable. d. Accessing an instance variable that is shadowed by a local variable

d. Accessing an instance variable that is shadowed by a local variable

10. The statement s1.startsWith("art") has the same result as which of the following? a. s1.regionMatches(0, "art", 0, 3); b. s2 = s1.getChars(0, 3); s2.equals("art"); c. s1.regionMatches(true, 0, "art", 0, 3); d. All of the above

d. All of the above

23. Consider the abstract superclass below: public abstract class Foo { private int a; public int b; public Foo(int aVal, int bVal) { a = aVal; b = bVal; } public abstract int calculate(); } Any concrete subclass that extends class Foo: a. Must implement a method called calculate. b. Will not be able to access the instance variable a. c. Neither (a) nor (b). d. Both (a) and (b).

d. Both (a) and (b).

35. Constructors: a. Initialize instance variables. b. When overloaded, can have identical argument lists. c. When overloaded, are selected by number, types and order of types of parameters. d. Both (a) and (c).

d. Both (a) and (c).

20. Which of the following statements is true? a. The code in a finally block is executed only if an exception occurs. b. The code in a finally block is executed only if an exception does not occur. c. The code in a finally block is executed only if there are no catch blocks. d. None of the above are true.

d. None of the above are true.

24. 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? a. Method originalMethod cannot be overridden in class C—once it has been implemented in concrete class B, it is implicitly final. b. Method originalMethod must be overridden in class C, or a compilation error will occur. c. If method originalMethod is not overridden in class C but is called by an object of class C, an error occurs. d. None of the above.

d. None of the above.

52. Which statement prints the floating-point value 123.456 right justified with a field width of 10? a. System.out.printf("%d10.3", 123.456); b. System.out.printf("%10.3d", 123.456); c. System.out.printf("%f10.3", 123.456); d. System.out.printf("%10.3f", 123.456);

d. System.out.printf("%10.3f", 123.456);

19. Which of the following statements about try blocks is true? a. The try block must be followed by at least one catch block. b. The try block must be followed by a finally block. c. The try block should contain statements that may process an exception. d. The try block should contain statements that may throw an exception.

d. The try block should contain statements that may throw an exception.

37. Consider the code segment below. Which of the following statements is false? int[] g; g = new int[ 23 ]; a. The first statement declares an array reference. b. The second statement creates the array. c. g is a reference to an array of integers. d. The value of g[ 3 ] is -1.

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

25. Interfaces can have _______ methods. a. 0 b. 1 c. 2 d. any number of

d. any number of

48. Which of the following promotions of primitive types is not allowed to occur? a. char to int. b. int to double. c. short to long. d. double to float.

d. double to float.

15. Consider the Java segment: String line1 = new String("c = 1 + 2 + 3") ; StringTokenizer tok = new StringTokenizer(line1, "+="); String foo = tok.nextToken(); String bar = tok.nextToken(); The values of foo and bar are: a. foo is "c ", bar is " = ". b. foo is "c", bar is " ". c. foo is " = ", bar is " + ". d. foo is "c ", bar is " 1 ".

d. foo is "c ", bar is " 1 ".

4. Path method ________ returns the String name of a file or directory without any location information. a. getStringName b. getFileOrDirectoryName c. getDirectoryName d. getFileName

d. getFileName

28. Inheritance is also known as the _______ a. knows-a relationship. b. has-a relationship. c. uses-a relationship. d. is-a relationship.

d. is-a relationship.

58. Which primitive type can hold the largest value? a.int b.long c.float d.double

d.double

51. Which of the following will count down from 10 to 1 correctly? a.for ( int j = 10; j <= 1; j++ ) b.for ( int j = 1; j <= 10; j++ ) c.for ( int j = 10; j > 1; j-- ) d.for ( int j = 10; j >= 1; j-- )

d.for ( int j = 10; j >= 1; j-- )


Kaugnay na mga set ng pag-aaral

four parts of the introduction, p. 32

View Set

Chapter 14 Alcohol Addiction and Families

View Set

Systems Analysis and Design - Chapter 4

View Set

Chapter 22 MedSurg Exam 4 Review (Evolve Questions)

View Set