CIS355A Week5 Quiz

Question 1.1. (TCOs 1–8) Which of the following statements is false? (Points : 3)

        You can create a FileInputStream/FileOutputStream from a File object or a file name using FileInputStream/FileOutputStream constructors.

        A java.io.FileNotFoundException would occur if you attempt to create a FileOutputStream with a nonexistent file.

        All methods in FileInputStream/FileOutputStream are inherited from InputStream/OutputStream.

        The return value -1 from the read() method signifies the end of file.

        A java.io.FileNotFoundException would occur if you attempt to create a FileInputStream with a nonexistent file.

 

 

Question 2.2. (TCOs 1–8) Given a graphics object g, to draw a filled arc with radius 20 centered at (50, 50) and start angle 0 and spanning angle 90, you use _____ (Points : 3)

        g.fillArc(50, 50, 20, 20, 0, 90).

        g.fillArc(50, 50, 40, 40, 0, 90).

        g.fillArc(30, 30, 40, 40, 0, Math.toRadian(90)).

        g.fillArc(30, 30, 40, 40, 0, 90).

        g.fillArc(50, 50, 40, 40, 0, Math.toRadian(90)).

 

 

Question 3.3. (TCOs 1–8) Which of the following statements is false? (Points : 3)

        Invoking repaint() causes paintComponent to be invoked by the JVM.

        The paintComponent method is automatically invoked by the JVM. You should never invoke it directly.

        Whenever a GUI component is displayed, its graphics object is automatically created.

        You may create a graphics object using new Graphics().

 

 

Question 4.4. (TCOs 1–8) After the following program is finished, how many bytes are written to the file t.dat?

import java.io.*;

public class Test {

  public static void main(String[ ] args) throws IOException {

    DataOutputStream output = new DataOutputStream(

      new FileOutputStream(“t.dat”));

    output.writeChar(‘A’);

    output.close();

  }

} (Points : 3)

        8 bytes

        2 bytes

        4 bytes

        none of the above

 

 

Question 5.5. (TCOs 1–8) Which of the following is the legal mode for creating a new RandomAccessFile stream? (Points : 3)

        “rw”

        “rwx”

        “w”

        ‘r’

 

 

Question 6.6. (TCOs 1–8) Suppose you enter 34.3 57.8 789, then press the ENTER key. Analyze the following code.

Scanner scanner = new Scanner(System.in);

int intValue = scanner.nextInt();

int doubleValue = scanner.nextInt();

String line = scanner.nextLine(); (Points : 3)

        After the last statement is executed, line contains characters ‘7’, ‘8’, ‘9’.

        After the last statement is executed, line contains characters ‘7’, ‘8’, ‘9’, ‘\n’.

        After the last statement is executed, intValue is 34.

        The program has a runtime error because 34.3 is not an integer.

 

 

Question 7.7. (TCOs 1–8) Assume s is “ABCABC”, the method _____ returns a new string “aBCaBC.” (Points : 3)

        s.toLowerCase(s)

        s.toLowerCase()

        s.replace(“ABCABC”, “aBCaBC”)

        s.replace(‘a’, ‘A’)

        s.replace(‘A’, ‘a’)

 

 

Question 8.8. (TCOs 1–8) Which correctly creates an array of five empty Strings? (Points : 3)

        String[ ] a = new String [5]; for (int i = 0; i < 5; a[i++] = null);

        String[ ] a = new String [5];

        String[5] a;

        String[ ] a = {“”, “”, “”, “”, “”};

 

 

Question 9.9. (TCOs 1–8) Which of the following statements is preferred to create a string “Welcome to Java”? (Points : 3)

        String s; s = “Welcome to Java”;

        String s = “Welcome to Java”;

        String s = new String(“Welcome to Java”);

        String s; s = new String(“Welcome to Java”);

 

 

Question 10.10. (TCOs 1–8) Suppose s is a string with the value java. What will be assigned to x if you execute the following code?

char x = s.charAt(4); (Points : 3)

        ‘v’

        ‘a’

        Nothing will be assigned to x, because the execution causes the runtime error StringIndexOutofBoundsException.

 

 

Leave a Comment

Your email address will not be published. Required fields are marked *