Exam

Card Set Information

Author:
wgaynor
ID:
269764
Filename:
Exam
Updated:
2014-04-08 14:29:29
Tags:
java
Folders:
java
Description:
Mock Exam
Show Answers:

Home > Flashcards > Print Preview

The flashcards below were created by user wgaynor on FreezingBlue Flashcards. What would you like to do?


  1. 1.  Which statement is not true about package statements?

    A. Package statements are optional.

    B. Package statements are limited to one per source file.

    C. Standard Java coding convention for package names reverses the domain name of the organization or group creating the package.

    D. The package names beginning with javas.* and javaw.* are reserved
    • D is correct because the statement is false. Package names beginning withjavas.* and javaw.* are NOT reserved and can be freely used.       
    • However, the package names beginning with java.* and javax.* ARE reserved.
  2. 2. Which statement represents a valid statement that will allow for the inclusion of classes from the java.util package?
    A. import java.util;
    B. #include java.util;
    C. #include java.util.*;
    D. import java.util.*;
    D. To include all of the classes in the java.util package, the import statement is used, followed by the package name, including an asterisk.
    (this multiple choice question has been scrambled)
  3. 3. List four interfaces of the Collections API.
    A. ArrayList, Map, Set, Queue
    B. List, Map, Set, Queue
    C. List, Map, HashSet, PriorityQueue
    D. List, HashMap, HashSet, PriorityQueue
    B. Interfaces of the Collections API include the List, Map, Set, and Queue APIs

    A, C, and D are incorrect. ArrayList, HashMap, HashSet, and PriorityQueue are all concrete classes and are considered to be implementations of their respective interfaces.
    (this multiple choice question has been scrambled)
  4. 4. Which class in the java.io package allows for the reading and writing of files to specified locations within a file?
    A. RandomAccessFile
    B. FilenameFilter
    C. File
    D. FileDescriptor
    A. Class RandomAccessFile allows for the reading and writing of files to specified locations.

    A, B, and C are incorrect. A is incorrect because the File class provides a representation of file and directory pathnames. B is incorrect because theFileDescriptor class provides a means to function as a handle for opening files and sockets. C is incorrect because the FilenameFilter interface defines the functionality to filter filenames.
    (this multiple choice question has been scrambled)
  5. 5. Which MS Windows–based command-line utility will allow you to run the Java interpreter without launching the console window?
    A. javaw
    B. interpw
    C. java -wo
    D. jconsole
    A. The javaw utility used on MS Windows machines will run the Java interpreter without a console window.

    B, C, and D are incorrect. B is incorrect because there is no interpw command. C is incorrect because there is no -wo switch. D is incorrect because jconsole is a management and monitoring application, not an interpreter.
    (this multiple choice question has been scrambled)
  6. 6. What is the correct import package needed to use the ArrayList class?
    A. import java.net.*;
    B. This is a trick question, because it is part of the java.lang package that is imported automatically.
    C. import java.util.*;
    D. import javax.swing.*;
    E. import java.io.*;
    F. import java.awt.*;
    C. ArrayList is located in the java.util package.       

    A, B, C, D, and F are incorrect. The import statements in A, B, C, and D import packages that do not contain the ArrayList class. F is incorrect because ArrayList is not part of the java.lang package.
    (this multiple choice question has been scrambled)
  7. 7. Of the following packages, which contain classes for building a graphical interface? (Choose all that apply.)
    A. java.awt
    B. java.io
    C. java.net
    D. javax.swing
    E. java.util
    A and D. Swing and AWT are the two Java graphical toolkits and are included in thejava.awt and javax.swing packages.       

    B, C, and E are incorrect. B is incorrect because java.io is used for input and output type jobs. C is incorrect because java.net is used for network connections. E is incorrect because the java.util package contains classes such as Vector, ArrayList, Random, and many more, but no classes for building a GUI.
  8. 8. Which of the following statements is correct?
    A. A Java class does not extend superclasses; it implements them.
    B. A Java class cannot extend any superclasses.
    C. A Java class can extend only one superclass.
    D. A Java class can extend multiple superclasses.
    C. A Java class can extend only one superclass.       

    B, C, and D are incorrect. B is incorrect because a Java class cannot extend multiple superclasses. C is incorrect because Java can extend a superclass. D is incorrect because a class will extend a class but implements an interface.
    (this multiple choice question has been scrambled)
  9. 9. You have created a set of classes for your company and would like to include them in a package. Which one of the following would be a valid package name?
    A. com.your company name
    B. your company name
    C. com.your_company_name
    D. java.your company name
    E. java.your_company_name
    C. A package name should be your reverse domain name.       

    A, B, C, and D are incorrect. A, B, and C are incorrect because they contain spaces that are not valid characters in package names. C and D are incorrect because java*is reserved for official Java packages.
    (this multiple choice question has been scrambled)
  10. 10. What is the result of running the following command: javac Simulator.java
    A. The simulator program would be executed.
    B. A bytecode file Simulator.java would be created.
    C. An error would be displayed because this is the wrong syntax.
    D. A bytecode file Simulator.class would be created.
    D. javac is the Java compiler. It is used to compile a Java source file into a bytecode file. A bytecode filename has the .class extension.
    (this multiple choice question has been scrambled)
  11. 11. Of the following, which types of statements must be used to count the number of nickels in a String array of various coins?
    A. Assignment
    B. Assertion
    C. Iteration
    D. Conditional
    A, C, and D. The code will need to iterate through an array, test the condition to see if the array value equals a "nickel" string, and perform an assignment to increase the value of the counter.

    B is incorrect. Assertion statements are used to evaluate whether code is functioning as expected.
  12. 12. The following short code segment contains two errors. What are they?
    int a = 1;
    does
    a = 2;
    while (a == 1)

    A. The declaration for the variable a must occur within the do-while statement.
    B. Braces must be included for do-while statements even when only one statement is enclosed.
    C. There is no does keyword; only the do keyword is allowed as part of a conditional statement
    D. The do-while statement must end with a semicolon.
    • C and D.
    • C contains an error because it needs a do-while statement, not an invaliddoes-while statement.
    • D contains an error because the do-while statement must end with a semicolon.       

    • A and B are incorrect.
    • A is incorrect because declarations must occur outside of thedo-while statement. As such, the declaration was represented correctly in the code segment.
    • B is incorrect because when there is only one statement enclosed in the do-while statement, it does not need to be enclosed in braces.
  13. 13. The continue and break statements are allowed within what types of statements?
    A. Loop statements
    B. All conditional statements
    C. The switch statement
    D. Expression statements
    A and C. Iteration statements and switch statements can include continue and break statements. Iteration statements, known as loops, include the for loop, enhanced for loop, while loop, and do-while loop.

    B and D are incorrect. The only conditional statement that can include the breakand continue statement is the switch statement. There is no logical reason to continue or break out of an expression statement.
  14. 14. Select the commenting style that uses symbols designed to work with Javadoc.
    A. /** @author Robert ;
    B. // @author Robert
    C. /* @author Robert */
    D. /** @author Robert */
    D. Javadoc comments are included between /** and */.       

    A, B, and D are incorrect. A is incorrect because it shows a normal line comment. B is incorrect because it shows a normal block comment. D is incorrect because this Javadoc comment isn’t terminated properly; it must terminate with */.
    (this multiple choice question has been scrambled)
  15. 15. Given the following code segments, which answer is NOT a valid Java implementation because it won’t compile?

    A. int variableA = 10;float variableB = 10.5f;int variableC = variableA + variableB;

    B. int variableA = 10;float variableB = 10.5f;float variableC = variableA + variableB;

    C. byte variableA = 10;float variableB = 10.5f;float variableC = variableA + variableB;

    D. byte variableA = 10;double variableB = 10.5f;double variableC = variableA + variableB;
    A. This implementation will not compile, because adding an int and a floattogether does not yield an integer literal as is expected for the assignment.       

    B, C, and D are incorrect. These answers all have code that will compile because the code has necessary implicit casts that occur within their expression and assignment statements.
  16. 16. Which of the following code segments contain invalid Java statements? It can be assumed that all variables have been properly declared and initialized. (Choose all that apply.)
    A. int x = getSize();

    B. int a = 1;int b = 2;int c = a = b;

    C. int mostPoints = 100;int score = 87;if ( mostPoints = score ) { setWinner(); }

    D. boolean stillRunning = true;if ( stillRunning = true ) { run(); }
    C. This statement is not valid. The variable mostPoints is an int. It cannot be placed in an if statement without a relational operator.       Remember that the assignment of primitive literals evaluate to primitive values.       

    A, B, and D are incorrect. A is incorrect because the statement is valid—it is a simple assignment. B is incorrect because the code segment is valid. It is legal to have more than one variable assigned on a line as seen in the last line of B. The last line of this segment sets a equal to b, and then c equal to a. D is incorrect because the segment is valid. Because stillRunning is a boolean value, a relational operator is not needed. Note that while this answer is legal and compiles, it may not be what was intended, as relational operators are typically used in if statements.
  17. 17. Which conditional statement would be best suited for determining the flow of code based on an int that may have a value between 1 and 10? The program must react differently for each value.
    A. multiple if statements
    B. switch statement
    C. if-else statement
    D. if-else-if statement with multiple if-elses
    B. A switch statement is best suited for situations where multiple possible conditions exist that must be accounted for.       

    A, B, and C are incorrect. A is incorrect because the if-else statement only allows two conditions and this question calls for ten conditions. B and C are incorrect because a combination of if statements should be used only when limited conditions are being checked.
    (this multiple choice question has been scrambled)
  18. 18. What Java code is used for declaring and initializing a null object?
    A. Object object = new Object();
    B. Object object = null();
    C. Object object = null;
    D. Object object = new Object(null);
    C. To create or set an object to null, the null keyword can be used. Objects are also set to null when they are declared but not yet initialized.       

    A, B, and D are incorrect. A is incorrect because when the new keyword is used, it initializes the object to a new non-null object. B is incorrect for the same reason as A, plus the Object constructor does not accept any arguments. D is incorrect becausenull() is a made up method and is invalid in Java.
    (this multiple choice question has been scrambled)
  19. 19.  Given the following code segment, what would be the output?

    int value1 = 5;
    int value2 = 7;
    boolean bool1 = true;
    boolean bool2 = false;
    if (bool1 && ((value1 < value2) || bool2) && !(bool2)) {
      System.out.println("Result set one");
    }
    else {
      System.out.println("Result set two");
    }

    A. Result set one
    B. Result set two
    A. The expression in the if statement evaluates to true. This expression is actually easier than it looks considering that bool1 is on the left side of a short-circuit operator.       

    B is incorrect.
  20. 20.  What is the output for the following Java code segment? The text in parentheses is meant to describe the whitespace and is not part of the output.

    String fiveSpaces = "     ";
    String SCJA = "SCJA";
    String lineToDisplay = "fiveSpaces + SCJA + fiveSpaces";
    System.out.println(lineToDisplay.trim());

    A. SCJA (no trailing and leading spaces)
    B         SCJA         (five leading and trailing spaces)
    C.         SCJA         (four leading and trailing spaces)
    D. fiveSpaces + SCJA + fiveSpaces
    D. This is a trick question. Notice the quotation marks used to initialize thelineToDisplay variable. The quotation marks indicate that everything between them is passed as a string. This is not concatenating the variables, as it might seem at first. The trim method removes whitespace from the beginning and end of a string. The string is everything between the quotation marks. Since the string neither starts nor ends with spaces, nothing is trimmed.       

    A, B, and C are incorrect. If the quotation marks were not present, A would have been correct.
  21. 21.  Given:

    public class StringModifier {
      public static void main (String[] args) {
        String a = "Supercalifragilisticexpialidocious!";
        String b = a.substring(x,y);
        char [] c = {a.charAt(u), a.charAt(v)};
        System.out.print(b + String.valueOf(c));
      }
    }

    What integer declarations are needed to print the string fragile!?

    A. int x = 10; int y = 16; int u = 4; int v = 35;
    B. int x = 8; int y = 14; int u = 2; int v = 33;
    C. int x = 8; int y = 15; int u = 5; int v = 32;
    D. int x = 9; int y = 15; int u = 3; int v = 34;
    D. The integer declarations supplied in answer D causes the printing of fragile!       

    A, B, and C are incorrect. A is incorrect because the integer declarations supplied causes the printing of ifragips. B is incorrect because the integer declaration causes a       StringIndexOutOfBoundsException exception to be thrown since there are only 34 characters to be accessed, not 35. C is incorrect because the integer declaration causes the printing of ifragilcu.
    (this multiple choice question has been scrambled)
  22. 22.  You want to remove all leading and trailing whitespace from a string. Which method invocation will allow this to occur?
    A. stringName.trim(' ');
    B. stringName.trim();
    C. stringName.trimWhiteSpace();
    D. stringName.trim(" ");
    B. The method is called trim and it receives no arguments.       

    B, C, and D are incorrect. B and C are incorrect because these methods inappropriately try to receive arguments. D is incorrect because there is notrimWhiteSpace method.
    (this multiple choice question has been scrambled)
  23. 23.  From highest precedence to lowest, which list of operators is ordered properly?

    A. +, *, &&, =
    B. *, &&, +, =
    C. *, =, &&, +
    D. *, +, &&, =
    D. In order of precedence, the following is correct: multiplication (*), addition (+), conditional AND (&&), assignment (=).       

    B, C, and D are incorrect because they are improperly ordered.
    (this multiple choice question has been scrambled)
  24. 24.  Given the following code that uses the modulus operator, what will be printed?
    System.out.print((24 % 8) + (10 % 7) + (100 % 99) + (38 % 6));

    A. 5
    B. 6
    C. 0302
    D. 0312
    B. 24 % 8 = 0, 10 % 7 = 3, 100 % 99 = 1, 38 % 6 = 2. Added together, 0 + 3 + 1 + 2 = 6
    (this multiple choice question has been scrambled)
  25. 25.  Given the following code segment testing the equality of two strings, what will be printed?



    1.  String string = "dollar bill";
    2.  string.replace("Dollar bill", "Silver dollar");
    3.  if ("Dollar bill".equals(string) {
    4.    System.out.prinln ("I have a dollar bill.");
    5.  }  else {
    6.    System.out.println ("I have a silver dollar.");
    7.  }

    A. I have a dollar bill.

    B. I have a silver dollar.

    C. The code will not compile because of an error at line 2.

    D. The code will not compile because of an error at line 3.
    A. Strings are immutable, so the statement at line 2 does not affect the string; it’s as if nothing ever happened. Explicitly overwriting the value of the string object would have changed the result: string = string.replace("Dollar bill", "Silver dollar");       

    B, C, and D are incorrect. B is incorrect because the condition results in true. C and D are incorrect because the code compiles fine.
  26. 26.  Which append declaration does not exist in Java 7?
    A. public StringBuilder append (long l) {…}
    B. public StringBuilder append (double d) {…}
    C. public StringBuilder append (float f) {…}
    D. public StringBuilder append (int i) {…}
    E. public StringBuilder append (short s) {…}
    E. The append declaration that includes a short is not included in the StringBuilder class. This is because the short value is automatically casted to an integer when passed for integer literal. Therefore, the following declaration will also handle a short argument:public StringBuilder append (int i) {…}       

    B, C, D, and E are incorrect. B is incorrect because the append declaration that includes an int is included in the StringBuilder class. C is incorrect because the append declaration that includes a long is included in the StringBuilder class. D is incorrect because the appenddeclaration that includes a float is included in the StringBuilder class. E is incorrect because the append declaration that includes adouble is included in the StringBuilder class.
    (this multiple choice question has been scrambled)
  27. 27.  Given:       
    System.out.println("A" + 1 + (1 + 1) + 1);
    What will print?

    A. A121
    B. A13
    C. A31
    D. Compilation error
    E. A1111
    A. Precedence occurs first with the enclosed 1+1 equation. The evaluation then continues at the beginning of the expression with the string, and each value after the string is appended as a string, resulting in A121.       

    A, C, and D are incorrect because the resultant output is A121. E is incorrect because there is no compilation error.
    (this multiple choice question has been scrambled)
  28. 28.  Given:
    StringBuilder s = new StringBuilder ("magic");
    s.append("al").replace(1, 3, "us").matches("musical");
    System.out.println(s);

    What will be printed to standard out?

    A. magical
    B. musical
    C. true
    D. The code will not compile.
  29. Given:

    boolean value = true;
    System.out.print( true || (value=false));
    System.out.println(", " + value);

    What is printed to standard out?
    A. false, true
    B. true, false
    C. true, true
    D. false, true
    C. Since conditional OR (||) is a short-circuit operator, as soon as the left operand evaluates to true, the right operand is not evaluated.       Therefore, true, true is printed to standard out.       

    B, C, and D are incorrect because true, true is printed to standard out.
    (this multiple choice question has been scrambled)
  30. Given:

    String value1 = "null";
    String value2 = "null";
    System.out.println(value1.equalsIgnoreCase(value2));

    What result will be seen upon compilation and/or execution?
    A. equalsIgnoreCase is not a valid method name and causes a compilation error.
    B. "false" is printed to standard out.
    C. A NullPointerException is thrown at runtime.
    D. "true" is printed to standard out.
    D. Both values of null are the same, so the evaluation prints "true". In this scenario, the method equalsIgnoreCase provided more functionality than was needed, and could have been replaced with just the equals method to return "true".       

    A, B, and D are incorrect. A is incorrect because there are no null values. The values are actually strings assigned the character sequence of "null". B is incorrect because equalsignoreCase is a valid method name so no compilation error will occur. D is incorrect because "true" is printed.
    (this multiple choice question has been scrambled)
  31. 31.  Given the following code segment, what would be the output?

    int a = 0;
    int b = 0;
    int c = 0;
    System.out.println(a++ + " " + --b + " " + c++ + " " + a + " " + b);

    A. 0 -1 0 1 -1
    B. 0 0 0 0 0
    C. 1 0 1 1 -1
    D. 1 0 1 0 0
    A. D is correct relative to the expected behavior of the decrement and increment operators       

    A, B, and C are incorrect.
    (this multiple choice question has been scrambled)
  32. 32.  Which of the following string classes create mutable strings? (Choose all that apply.)
    A. String
    B. StringBuffer
    C. StringBuilder
    B and C. Both the StringBuffer class and StringBuilder class create strings that are mutable.       

    A is incorrect. The String class creates immutable strings.
  33. 33.  Choose the simplest data structure to use to store multiple like variables that could then be accessed by an index.
    A. Array
    B. Object
    C. Enumeration
    D. Primitive
    A. An array is used to store multiple variables that are the same data type variables.       

    A, B, and D are incorrect. A is incorrect because primitives can make up the contents of an array but an array is required to hold them. B is incorrect because an object may be used to simulate an array but it is much more complex than using just an array. D is incorrect because enumerations are used to limit the possible values that can be stored in a variable to a predefined list.
    (this multiple choice question has been scrambled)
  34. 34.  Which of the statements are correct? (Choose all that apply.)
    A. 3.0 is a valid literal for an int.
    B. 3.0 is a valid literal for a float.
    C. 3 is a valid literal for an int.
    D. 3 is a valid literal for a float.
    E. 3f is a valid literal for an int.
    F. 3f is a valid literal for a float.
    C, D, and F. C is correct because an int is used to store an integer number. D is correct because the compiler will automatically convert 3 to a floating-point number. F is also correct because when an f is appended to a number, it implies that that number is a floating-point number, even if it does not have a decimal.       

    A, B, and E are incorrect. A is incorrect because an int cannot store a decimal number. B is incorrect because 3.0f would be a valid literal for a float, but 3.0 would not (the compiler treats 3.0 as a double). E is incorrect because an int cannot have fappended to it.
  35. 35.  What literal values are acceptable to use with the boolean primitive?
    A. true, false, TRUE, and FALSE
    B. TRUE and FALSE
    C. true, false, and null
    D. true and false
    D. Valid literal values for the boolean primitive include true and false.       

    B, C, and D are incorrect. TRUE, FALSE, and null are all invalid literals for theboolean primitive. The null value is a valid literal for the Boolean wrapper class.
    (this multiple choice question has been scrambled)
  36. 36.  Which code segments related to enumerations will result in a compiler error? (Choose all that apply.)

    A. enum Coin {PENNY, NICKEL, DIME, QUARTER}Coin coin = Coin.NICKEL;

    B. enum Coin {PENNY, NICKEL, DIME, QUARTER}Coin coin;coin = Coin.NICKEL;

    C. enum Coin {PENNY, NICKEL, DIME, QUARTER}Coin coin = NICKEL;

    D. enum Coin {penny, nickel, dime, quarter}Coin coin = Coin.NICKEL;
    C and D. C and D are correct because the associated code segment will not compile. The code in C does not compile because NICKEL by itself cannot be resolved; the code must read Coin.NICKEL. The code in D does not compile due to the case mismatch between nickel and NICKEL.      

    A and B are incorrect because they are all valid, compilable code segments.
  37. 37.  Consider the following declarations. Which declaration has an element name that does not conform to standard naming conventions?

    A. Integer IntName; // variable
    B. public class Name {…} // class
    C. void getName () {…} // method
    D. static final int varName; // variable
    A. B is correct because the proper naming convention is not used. All variables should start with a lowercase letter—thus, IntName should read intName.      

    A, C, and D are incorrect because they make use of the proper naming conventions. Variable and method names should appear in all lowercase characters, using camel case where appropriate. Class, interface, and enumeration names should start with an uppercase character, followed by lowercase characters and camel case where appropriate. Note that “camel case” refers to the capitalization of the first letter of appropriate words within an identifier.
    (this multiple choice question has been scrambled)
  38. 38.  Java 7 added the allowance of a special character to indentify places. Which declaration is correct?
    A. int investment = 1x000x000;
    B. int investment = 1-000-000;
    C. int investment = 1_000_000;
    D. int investment = 1^000^000;
    C. The underscore is used to identify places in numeric values.       

    A, C, and D are incorrect because x, ^, and – are incorrect; they are not used to identify places in numeric values.
    (this multiple choice question has been scrambled)
  39. 39.  What are the wrapper classes for the primitives boolean, char, short, int, and double?
    A. Boolean, Char, Short, Integer, Double
    B. Boolean, Character, Short, Int, Double
    C. Boolean, Char, Short, Int, Double
    D. Boolean, Character, Short, Integer, Double
    D. The wrapper class reference types use the full spelling of the type names beginning with a capital letter. The full set of wrapper classes is Boolean, Character, Byte, Short, Integer, Long, Float, and Double.       

    A, B, and C are incorrect. Classes named Char and Int do not exist.       The appropriate wrapper classes are named Character and Integer.
    (this multiple choice question has been scrambled)
  40. 40.  Which of the following statements contain literal values?
    A. int maxHorsePower = 170;
    B. float currentHorsePower = (float) maxHorsePower;
    C. float idleHorsePower = ((float) currentHorsePower) / 10);
    D. System.out.println("Current HP: " + currentHorsePower);
    A, C, and D. The 170 in A, the 10 in C, and the "Current HP: " in D are all literals.       

    B is incorrect because this statement does not contain any values that are literals.
  41. 41.  What is the correct way to initialize a variable declared as a Penguin as a new Penguin object?
    A. Penguin p = Penguin();
    B. Penguin p;
    C. Penguin p = new Penguin[];
    D. Penguin p = new Penguin();
    D. It uses the correct syntax to declare and create a new object.       

    A, C, and D are incorrect. A is incorrect because it declares a Penguin object but does not initialize it. C is incorrect because [] is used when it should be (). D is incorrect because it omits the new keyword.
    (this multiple choice question has been scrambled)
  42. 42.  Which of the following are primitive data types? (Choose all that apply.)
    A. int
    B. boolean
    C. char
    D. Float
    E. String
    A, B, and C. Primitive types include int, boolean, and char. Notice that they all start with a lowercase letter.       

    D and E are incorrect. D is incorrect because Float is the wrapper class for a float. E is incorrect because String is a reference to the String class. The capital F inFloat, and in any other data type, should signal that this is an object.
  43. 43.  Which code example makes use of arrays without producing a compiler or runtime error?

    A. public class Actor {
          String [] characterName = new String [3];
          {
            characterName[0] = "Captain Video";
            characterName[1] = "Quizmaster";
            characterName[2] = "J.C. Money";
            characterName[3] = "Jersey Joe";
          }
       }
    B. public class Actor {     
         String [] characterName = new String [1..4];     
         {
               characterName[0] = "Captain Video";
               characterName[1] = "Quizmaster";  
               characterName[2] = "J.C. Money";  
               characterName[3] = "Jersey Joe";
          }
       }

    C. public class Actor {
          String characterName = new String [4];
          {
            characterName[0] = "Captain Video"; 
            characterName[1] = "Quizmaster";  
            characterName[2] = "J.C. Money"; 
            characterName[3] = "Jersey Joe"; 
        }
       }

    D. public class Actor {
          String [] characterName = new String [4];
          {
            characterName[0] = "Captain Video";
            characterName[1] = "Quizmaster";
            characterName[2] = "J.C. Money";
            characterName[3] = "Jersey Joe";
          }
       }
    D. The array declaration and element assignments are used appropriately and compile without error.      

     A, B, and C are incorrect. A compiles with an ArrayIndexOutOfBoundsExceptionexception thrown at runtime. This is because the array index characterName[3] is out of bounds. B fails to compile because only an integer is expected in the box brackets. C fails to compile because the box brackets that are needed to create the array are missing.
  44. 44.  Class variables, also known as static fields, have only one instance in existence. Following standing naming conventions, which answer represents a class variable?
    A. ClassName.variableName
    B. variableName
    C. ClassName:variableName
    D. VariableName
    A. Class (static) variables are referenced by the class name and variable name, delineated by a period.       

    A, B, and C are incorrect. A and B are incorrect because variables are not qualified by a class name. C is incorrect because when referencing static variables, colons are not used for delineation.
    (this multiple choice question has been scrambled)
  45. 45.  What statement about local variables is true?
    A. Local variables are declared inside of methods and are not initialized with a default value.
    B. Local variables are declared outside of methods and are initialized with a default value.
    C. Local variables are declared outside of methods and are not initialized with a default value.
    D. Local variables are declared inside of methods and are initialized with a default value.
    A. Local variables are declared inside of methods and are not initialized with a default value.       

    A, B, and C are incorrect because they do not represent true statements about the properties of local variables.
    (this multiple choice question has been scrambled)
  46. 46.  Which declaration is a valid constructor for the class CreateMiracle?
    A. public CreateMiracle(String wish, String prayer){}
    B. public String createMiracle(String wish, String prayer) {}
    C. private String createMiracle(String wish) {}
    D. public void createMiracle(){}
    A. Constructors do not declare a return type, and therefore cannot return a value.       

    A, B, and C are incorrect because they are all method declarations.
    (this multiple choice question has been scrambled)
  47. 47.  You want to create a method that takes in three character parameters and returns a string. Which declaration is valid?

    A. public void doMethod (char a, char b, char c) {String s = null; return s;}

    B. public String doMethod (Char a, Char b, Char c) {String s = null; return s;}

    C. public String doMethod (char a, char b, char c) {String s = null; return s;}

    D. public string doMethod (char a, char b, char c) {String s = null; return s;}
    C. This is a perfectly formed method declaration.       

    A, B, and D are incorrect. A is incorrect because the declaration states that no value (void) is to be returned, but a string is. B is incorrect because the wrapper class for character is Character and not Char. D is incorrect because the return type of Stringdoes not start with a capital letter as it should. That is, "public string doMethod" should read "public String doMethod".
  48. 48.  Which modifiers can be applied to constructors? (Choose all that apply.)
    A. abstract
    B. final
    C. native
    D. strictfp
    E. static
    F. synchronized
    G. transient
    H. volatile
    I. None of the above
    I. The following modifiers cannot be used with constructors: abstract, final, native, strictfp, static, synchronized, transient, and volatile.       

    A, B, C, D, E, F, G, and H are incorrect, relative to the correct answer.
  49. 49.  If a constructor does not include an access modifier, which modifier will it use by default?

    A. A constructor that does not include an access modifier will always be declared as public.

    B. A constructor that does not include an access modifier will make use of the same access modifier that is used for its class.

    C. A compilation error will occur if a constructor does not include an access modifier.
    B. A constructor that does not include an access modifier will make use of the same access modifier that is used for its class.       

    A and C are incorrect. A is incorrect because constructors that do not include an access modifier are not always declared as public; they are declared the same as their class. C is incorrect because a compilation error will not occur if a constructor does not have an access modifier.
  50. 50.  What is the scope of an instance variable?

    A. The block of code in which it is declared.

    B. The method for which it is a parameter.

    C. The class in which it is declared.
    C. An instance variable is declared in the class, and outside of any method. It is in scope for the entire class, and its lifecycle is tied to that class.      

    A and B are incorrect. A is incorrect because local variables are in scope for the block of code in which they are declared. B is incorrect because method parameters are in scope for the entire method.
  51. 51.  What type of variable would be used to store the state of an object?
    A. Local variable
    B. Object variable
    C. Instance variable
    D. Method parameter
    C. Instance variables retain their value for the life of the object.       

    A, B, and D are incorrect. A is incorrect because a local variable is used for temporary items and stays in scope only until the block of code it is declared in is exited. B is incorrect because method parameters are the variables passed to a method as arguments. They are in scope only for that method. D is incorrect because the object variable does not exist.
    (this multiple choice question has been scrambled)
  52. 52.  Objects are passed by __________.
    A. Copy
    B. Reference
    C. Parameter
    D. Value
    B. Objects are always passed by reference. This means that a reference to the memory location of the object is passed to the method as opposed to a copy of the object.       

    A, C, and D are incorrect. A is incorrect because primitives are passed by value; this means that a copy of the variable is passed to methods. C and D are incorrect because copy and parameter are not valid ways to pass a variable.
    (this multiple choice question has been scrambled)
  53. 53.  What is the proper way to construct a method named displayImage that accepts two variables as arguments? The first variable is an array of ints called points, and the second is an Image object called image. This method does not return any variables.

    A. displayImage(int[], Image) {…}

    B. displayImage(int[] points, Image image) {…}

    C. void displayImage(int[], Image) {…}

    D. void displayImage(int[] points, Image image) {…}
    D. This method signature uses the void keyword to signify that there isn’t any data to return, and it contains the proper variable names.       

    A, B, and C are incorrect. A is incorrect because it is missing both the void keyword and the variable names. B is incorrect because it is missing the void keyword, and C is incorrect because it is missing the variable names.
  54. 54.  Which of the following statements is/are true about declaring a return type? (Choose all that apply.)

    A. The keyword void is used to signify that a method does not return any value.

    B. The use of the keyword void is optional if the method isn’t returning anything.

    C. If a method is returning an int, it must declare this by having int precede the method name in the method signature.

    D. If the return type is omitted, a method may or may not return data, depending on the condition inside of the method.
    A and C. A is correct because the void keyword means the method will not return any data. C is correct because the data type of the variable to be returned must precede the method name.       

    B and D are incorrect. B is incorrect because void is not optional if the method isn’t returning anything. The void keyword must be included in this condition. D is incorrect because a method must always declare a return type or void. If it is a constructor, it can omit a return type and it will be implied as void.
  55. 55.  Which association can be stated as “object A ‘uses-an’ object B”?

    A. Direct association

    B. Temporary association

    C. Composition association

    D. Aggregation association.
    B. In Temporary association indicates that object A “uses-an” object B.       

    A, C, and D are incorrect. A is incorrect because in direct association, one object “has-a” object. C is incorrect because in composition association, an object is “composed-of” another object. D is incorrect because in aggregation association, an object is “part-of” another object.
  56. 56.  Which statement is false about composition association?

    A. Composition association models a whole-part relationship, where the whole is responsible for the lifecycle of its parts.

    B. Composition association is a weaker association than aggregation association.

    C. Composition association maintains a “composed-of” relationship.

    D. Composition association is considered a class relationship.
    B. B is correct because it represents a false statement. Composition association is a stronger association than aggregation association.       

    A, C, and D are incorrect because they represent true statements. Composition association is a class relationship that models a whole-part relationship—also referred to as a “composed-of” relationship.
  57. 57.  Given the following simple code segment, what association is being used in regards to the Color.BLACK object relating to the Cat object?

    public Class Cat {
      public Color getColor () {
        return Color.BLACK;
      }
    }

    A. Navigability
    B. Realization
    C. Containment
    D. Dependency
    D. Temporary association, also known as dependency, exists when an object is temporarily used in a return statement.       

    A, B, and D are incorrect. Composition (containment), realization, and direct association (navigability) do not apply here.
    (this multiple choice question has been scrambled)
  58. 58.  Of the following associations, which has/have a weak relationship? (Choose all that apply.)

    A. Direct association
    B. Temporary association
    C. Composition association
    D. Aggregation association
    E. Complete association
    A, B, and D. Each of these associations is considered weak. This means they do not have any lifecycle responsibility, and if the relationship was lost, each object would still maintain its meaning.       

    C and E are incorrect. C is incorrect because composition association has a strong relationship. E is incorrect because complete association is a made-up term.
  59. 59.  What is true about composition association? (Choose all that apply.)

    A. It has a weak relationship.

    B. It has a strong relationship.

    C. It has lifecycle responsibility for the objects in the relationship.

    D. It can have a one-to-one multiplicity.

    E. It can have a one-to-many multiplicity.

    F. It can have a many-to-many multiplicity.
    B, C, D, and E. B is correct because composition relationships are a strong relationship. C is correct because the object that is composed of the other objects has a lifecycle responsibility for these objects. D and E are correct because one object can be composed of only another single object, or an object can be composed of many objects, like an array.       

    A and F are incorrect. A is incorrect because composition association is a strong relationship, not weak. F is incorrect because it is impossible to have a many-to-many relationship that is also a strong relationship.
  60. 60.  Which association does the following statement represent? “Object Z ‘has-an’ object Y.”

    A. Composition association
    B. Direct association
    C. Temporary association
    D. Aggregation association
    B. Direct association is a “has-a” relationship.       

    B, C, and D are incorrect. B is incorrect because temporary association is known as a dependency. C is incorrect because composition association is a “composed-of” relationship. D is incorrect because aggregation association is a “part-of” relationship.
    (this multiple choice question has been scrambled)
  61. 61.  In the following code segment, what is the relationship between the Store class and the MerchandiseItem class?

    public class Store {
        MerchandiseItem item;

        public Store() {
            item = new MerchandiseItem();
        }
    }

    A. Direct association

    B. Temporary association

    C. Composition association

    D. Aggregation association
    C. The Store object is “composed-of” the MerchandiseItem object. There is a strong relationship between the two classes, and the Store object has a lifecycle responsibility for the MerchandiseItem object.       

    A, B, and D are incorrect.
  62. 62.  In the following code segment, what is the multiplicity between the Album class and the Picture class?

    public class Album {
      Picture[] pictures;

      public Store() {
        pictures = new Picture[3];
        pictures[0] = new Picture();
        pictures[1] = new Picture();
        pictures[2] = new Picture();
      }
    }

    A. One-to-many
    B. One-to-one
    C. Many-to-many
    D. Many-to-most
    A. One Album object contains three Picture objects: one-to-many.       

    A, C and D are incorrect. A is incorrect because the array being contained in Albummeans this is a many relationship on at least one side. C is incorrect because this answer is impossible since the relationship is a composition. A composition association cannot be many-to-many. D is incorrect as there is no such thing as a many-to-most multiplicity.
    (this multiple choice question has been scrambled)
  63. 63.  Select the statement about interfaces that is correct.

    A. An interface can extend only one interface.

    B. An interface can extend more than one interface.
    B. An interface can extend more than one interface.       

    A is incorrect, because an interface can extend more than one interface.
  64. 64.  Which two statements related to inheritance are true?

    A. A concrete class can extend only one class.

    B. A concrete class can extend multiple classes.

    C. An interface can extend only one interface.

    D. An interface can extend multiple interfaces.
    A and D. A concrete class can extend only one class. An interface can extend multiple interfaces.       

    B and C are incorrect.
  65. 65.  Which Java elements can have abstract methods?

    A. Concrete classes
    B. Abstract classes
    C. Interfaces
    D. Packages
    B and C. Abstract classes and interfaces can have abstract methods.       

    A and D are incorrect. Concrete classes cannot have abstract methods. Packages in this context is an illogical answer.
  66. 66.  Which access modifier used in conjunction with getter and setter methods provides tight encapsulation?

    A. private
    B. public
    C. protected
    D. package-private
    A. Using the private access modifier in conjunction with getter and setter methods provides excellent encapsulation.       

    A, C, and D are incorrect. The package-private, protected, and public access modifiers do not provide for tight encapsulation.
    (this multiple choice question has been scrambled)
  67. 67.  Which statement is NOT true about abstract classes?

    A. An abstract class must include the keyword abstract in its declaration.

    B. Abstract classes can be instantiated.

    C. Abstract classes contain a mixture of implemented and abstract methods.

    D. When an abstract class is extended, all of its abstract methods must be implemented by a non-abstract subclass.
    B. B is correct because it’s a false statement. Abstract classes cannot be instantiated.       

    A, C, and D are incorrect because they are true statements. An abstract class must include the keyword abstract in its declaration. Abstract classes contain a mixture of implemented and abstract methods. When an abstract class is extended, all of its abstract methods must be implemented by the subclass.
  68. 68.  The private and protected access modifiers can be used with which entities? (Choose all that apply.)

    A. Classes

    B. Interfaces

    C. Constructors

    D. Methods

    E. Data members
    C, D, and E. The private and protected access modifiers can be used with constructors, methods, and data members.       

    A and B are incorrect. The private and protected access modifiers cannot be used with classes and interfaces.
  69. 69.  What can contain a mixture of implemented and unimplemented methods and must be extended to use?

    A. Concrete class
    B. Abstract class
    C. Java class
    D. Interface
    B is correct. An abstract class may contain implemented or unimplemented methods. An unimplemented method is also called an abstract method.       

    A, C, and D are incorrect. A is incorrect because a concrete class is a normal Java class that has all of its methods implemented. C is incorrect because the term “Java class” is a general term and does not apply to a specific type of class. D is incorrect because an interface is used to create a public interface for a class. It may not contain any implemented methods.
  70. 70.  If class A extends class B, what terms can be used to describe class A? (Choose all that apply.)

    A. Subclass
    B. Superclass
    C. Base class
    D. Parent class
    E. Child class
    A and E. A class that gains the functionality of other classes is called either a subclass or child class.       

    B, C, and D are incorrect. Superclass, base class, and parent class are all terms to describe the class that is being inherited.
  71. 71.  Which of the following methods use the proper naming convention for a getter accessing the variable totalPoints?

    A. TotalPoints()
    B. GetTotalPoints()
    C. getTotalpoints()
    D. getTotalPoints()
    D. A getter should be start with get, with a lowercase letter g, followed by the name of the variable starting with a capital letter.       

    A, B, and C are incorrect. A is incorrect because it is missing get. B is incorrect because the G in Get is capitalized, and it should be lowercase. C is incorrect because the p in points is lowercase and should be capitalized.
    (this multiple choice question has been scrambled)
  72. 72.  What access modifier, if any, should be used on all instance variables unless there is a particular reason not to use it?

    A. private
    B. package-private (default)
    C. protected
    D. public
    A. Instance variables should have the most restrictive modifier, which is private.       

    A, C, and D are incorrect. All of these access modifiers, including the default access level, are less restrictive than private.
    (this multiple choice question has been scrambled)
  73. 73.  What is the correct method signature for class Z if it uses interface A?

    A. public class Z inherits A{ … }

    B. public class Z extends A{ … }

    C. public class Z implements A{ … }

    D. public class Z uses A{ … }
    C. An interface is implemented.       

    A, B, and D are incorrect. A and D are incorrect because inherits and uses are made-up terms. B is incorrect because extends is used for classes, not interfaces.
  74. 74.  Which object-oriented principle is represented in the following declaration?       List<String> = new ArrayList<String>();

    A. Encapsulation

    B. Data abstraction

    C. Information hiding

    D. Polymorphism
    D. The code example is polymorphic.       

    A, B, and C are incorrect. Encapsulation, data abstraction, and information hiding are not represented in the code example.
  75. 75.  Which Java API allows for extensive use of the polymorphism OO principle?

    A. Logging API

    B. Collections API

    C. Concurrency API

    D. Networking API
    B. Java’s Collections API allows for extensive use of polymorphism.       

    A, C, and D are incorrect. Although polymorphism may be used in some areas of other Java APIs, the Collections API uses the feature quite commonly.
  76. 76.  Given:

    Short s1 = (Short)300;
    short s2 = (Short)300;
    short s3 = (short)300;
    short s4 = 300;

    Which statements are true? (Choose all that apply.)

    A. The declaration for s1 will compile.

    B. The declaration for s2 will compile.

    C. The declaration for s3 will compile.

    D. The declaration for s4 will compile.
    C and D. C is correct as an explicit short is applied. D is correct as an implicit shortis applied.       

    A and B are incorrect because you cannot use wrapper classes for explicit casts in this manner.
  77. 77.  Is it possible to cast a boolean type to another primitive type?

    A. Yes, it is possible to cast a boolean type to another primitive type.

    B. No, it is not possible to cast a boolean type to another primitive type.
    B. It is not possible to cast a primitive boolean type to another primitive type.       

    A is incorrect, because it is not possible to cast a boolean type to another primitive type.
  78. 78.  Given:     

    long longValue = 100;
    byte b = (byte)(short) (int)longValue; // narrowing

    byte byteValue = 100;
    long l = (long)(int)(short)byteValue; // widening

    System.out.println (b + l); 


    What will happen?

    A. "200" will be printed to standard out.

    B. The code will not compile due to issues with the declarations.

    C. The code will not compile due to an issue with the expression in the print statement.
    A. The widening and narrowing that occurs in the declaration is all acceptable. Also, implicit casts occur in the print statement.       

    B and C are incorrect as the code compiles fine without any compilation issues.
  79. 79.  What does it mean to “program to an interface”?

    A. Objects should be referenced by the interface they implement.

    B. Every object should implement at least one interface.

    C. Extending classes should be avoided in favor of using interfaces where possible.

    D. “Program to an interface” is another term for well-designed code.
    A. The statement “program to an interface” means that objects should be referenced by the interfaces that they implement. This creates more modular code.       

    B, C, and D are incorrect. None of these statements describe the concept.
  80. 80.  What are the design benefits of using polymorphism? (Choose all that apply.)

    A. Objects can be generalized as return types and method parameters.

    B. Less code needs to be written.

    C. Superclasses and subclasses can be used interchangeably.

    D. Polymorphism allows code to work with many different types of classes that all share some commonality, such as implementing the same interface.
    A and D. A is correct because polymorphism allows objects to act as other objects. The other objects are more general forms of the original object. D is correct because polymorphism also allows different classes that share some common functionality to be treated the same. They must all implement the same interface.       

    B and C are incorrect. B is incorrect because less code may be required, but this is not always the case. C is incorrect because objects can be referenced only in their more general form. An object cannot be used as a more specific object that extends it later.
  81. 81.  Polymorphism allows an object to be referred to as __________.(Choose all that apply.)

    A. Any base class it extends

    B. Any interface that is implemented

    C. A primitive data type

    D. An instance variableE. Any subclass that extends it
    A and B. Polymorphism allows an object to be referenced by a more general data type. A more general data type is either a base class it has extended or an interface it has implemented.       

    C, D, and E are incorrect. C and D are incorrect because an instance variable is a type of variable and has nothing to do with polymorphism. E is not correct because polymorphism cannot work when moving to more specific objects.
  82. 82.  Given that the FoodStore class extends the Store class, which of the following statements is/are valid? (Choose all that apply.)

    A. Store store = new FoodStore();

    B. FoodStore foodStore = new Store();

    C. Object obj = new FoodStore();

    D. Object obj = new Store();
    A, C, and D. A is correct because Store is a base class for FoodStore. C is correct because Object is a base class for FoodStore. The Object class is always the base class for every object. Store implicitly extends Object, Store is extended byFoodStore, and so FoodStore extends Object. D is correct because Store implicitly extends Object.       

    B is incorrect because a FoodStore object cannot be initialized with a more general class.
  83. 83.  What can be said about the following code?

    public interface Audible {...}
    public class Stereo implements Audible {...}
    public class Computer implements Audible{...}

    public class AudioSource() {
      public void startPlaying(Audible sound){...}
    }

    A. Audible is the base class for the Stereo and Computer classes.

    B. The startPlaying code should be changed so it uses polymorphism.

    C. Object is not a base class for the AudioSource class.

    D. The startPlaying method is using the design principle of “programming to an interface.”
    D. This is an example of programming to an interface. The startPlaying method will except as an argument any object that implements that Audible interface.       

    A, B, and C are incorrect. A is incorrect because Audible is an interface, not a class. B is incorrect because this example is already using polymorphism. C is incorrect because Object is the implied base class for all objects.
  84. 84.  Exceptions fall into three categories. Which is not an exceptions category?

    A. Check Exceptions
    B. Errors
    C. Assertions
    D. Unchecked Exceptions
    C. Assertions is not an exception category.       

    A, B and D are incorrect as Check Exceptions, Unchecked Exceptions, andErrors are all exception categories in Java.
    (this multiple choice question has been scrambled)
  85. 85.  Is it possible to catch any checked exception, unchecked exception, or error programmatically? Choose the statement that is true.

    A. It is possible to catch any checked exception, unchecked exception, or error.

    B. It is not possible to catch any checked exception, unchecked exception, or error, because you cannot catch errors.
    A. It is possible to catch any checked exception, unchecked exception, or error.       

    B is incorrect. All exceptions and errors can be caught, as they all inherit from theThrowable class. However, errors typically are not recoverable.
  86. 86.  Identify the statements that correctly orient the exceptions to the superclasses. (Choose all that apply.)

    A. NumberFormatException is a subclass of IllegalArgumentException.

    B. IllegalArgumentException is a subclass of NumberFormatException.

    C. IndexOutOfBoundsException is a subclass of ArrayIndexOutOfBoundsException.

    D. ArrayIndexOutOfBoundsException is a subclass of IndexOutOfBoundsException.
    A and D. NumberFormatException is a subclass of IllegalArgumentException.ArrayIndexOutOfBoundsException is a subclass of IndexOutOfBoundsException.       

    B and C are incorrect. B is incorrect because IllegalArgumentException is not a subclass of NumberFormatException, because IllegalArgumentException is the a superclass of NumberFormatException. C is incorrect becauseIndexOutOfBoundsException is not a subclass of ArrayIndexOutOfBoundsException, because IndexOutOfBoundsException is a superclass ofArrayIndexOutOfBoundsException.
  87. 87.  What is the name of the interface that methods must implement to utilize the new try-with-resources feature?

    A. AutoCloseable
    B. AutoClose
    C. There is no such feature.
    A. The AutoCloseable interface establishes a method definition close() that is used to close resources. This feature is used in conjunction with the new try-with-resources feature of Java SE 7.       

    A and C are incorrect. A is incorrect because there is no interface named AutoClose. C is incorrect because the try-with-resources feature does exist with JDK 1.7.
    (this multiple choice question has been scrambled)
  88. 88.  Which three classes are subclasses of the IOException class?

    A. FileNotFoundException
    B. SQLException
    C. ClassNotFoundException
    D. InterruptedIOException
    A, B, and D. FileNotFoundException, SQLException, and InterruptedIOExceptionare all subclasses of IOException.       

    C is incorrect. ClassNotFoundException is not a subclass of IOException; it is a subclass of ReflectiveOperationException.
  89. 89.  Both the getMessage and toString methods return information about exceptions that are caught. Which method returns the basic information as well as the class name of the exception?

    A. getMessage()
    B. toString()
    C. getData()
    D. toOutput()
    B. The toString method returns the basic information of an exception being caught as well as the class name—for example, “java.io.FileNotFoundException: … (The filename, directory name, or volume label syntax is incorrect)”.       

    A, C and D are incorrect. A is incorrect because the getMessage method returns only the basic information of an exception being caught, not the class name—for example, “… (The filename, directory name, or volume label syntax is incorrect)”. C is incorrect because there is no getData method. D is incorrect because there is no toOuputmethod.
    (this multiple choice question has been scrambled)
  90. 90.  Which of the following code fragments will throw a NumberFormatException?

    A. Integer.parseInt("INVALID");

    B. int e = (2 / 0);

    C. Object x = new Float("1.0"); Double d = (Double) x;

    D. String s = null; int i = s.length();
    A. Evaluation of the statement causes a NumberFormatException to be thrown.       

    B, C, and D are incorrect. B is incorrect because evaluation of the statement causes an ArithmeticException to be thrown. C is incorrect because evaluation of the statement causes a ClassCastException to be thrown. D is incorrect because evaluation of the statement causes a NullPointerExeption to be thrown.

What would you like to do?

Home > Flashcards > Print Preview