Computer Science Invitational Test S19D

Computer Science Invitational Test S19D Study Notes

Question 1: Equivalent of 20310

  • Choices:

    • A. 3158

    • B. 11010112 (Binary representation)

    • C. CB16 (Hexadecimal representation)

    • D. None.

    • E. More than one.

Question 2: Output of Code Segment

  • Code: out.print(14 + -8 * 3 - 12);

  • Calculation:

    • First calculate -8 * 3, which equals -24.

    • Then calculate 14 + -24 - 12 = 14 - 24 - 12 = -22.

  • Output Choices:

    • A. -22 (Correct)

    • B. 6

    • C. -54

    • D. 26

    • E. -14

Question 3: Output of printf Code Segment

  • Code: out.printf("$%10.3f", 789.456123);

  • Output Format Explanation:

    • Outputs in a field width of 10; 3 decimal places.

    • Expected output: $789.456. The '#'s represent blank spaces.

  • Output Choices:

    • A. $789.456 (Correct)

    • B. $###789.456

    • C. $790

    • D. $##########789.456

Question 4: Output of String Manipulation Code

  • Code:

  String sl="serendipity";
  out.print(sl.substring(5, 7) + s1.charAt(4) + s1.charAt(s1.lastIndexOf('e')));
  • Analysis:

    • sl.substring(5, 7) equals "di" (substring from index 5 to 6, inclusive).

    • s1.charAt(4) should be verified to see if s1 is defined.

    • s1.charAt(s1.lastIndexOf('e')) returns the character at the last 'e'.

  • Output Choices:

    • A. dipne

    • B. ndiee

    • C. dine (Correct)

    • D. din3

    • E. dien

Question 5: Boolean Code Output

  • Output Choices:

    • A. true

    • B. false

    • No additional information provided in the context of this question.

Question 6: Compile Status of Lines

  • Code:

boolean bl=false, b2=false, b3=false;
out.print(bl && !b2 || b3 ^ true);
long fl=Math.round(2.826); //line 1
int f2=Math.round(3.02); //line 2
double f3=Math.round(5.57f); //line 3
float f4=Math.round(5.46); //line 4
  • Analysis:

    • Line 1: Valid

    • Line 2: Invalid (Math.round returns long, cannot assign to int directly)

    • Line 3: Valid

    • Line 4: Valid

  • Output Choices:

    • A. line 1

    • B. line 2 (Correct)

    • C. line 3

    • D. line 4

    • E. None of the above. Each line will compile.

Question 7: Code Output for Mathematical Computation

  • Output Choices:

    • A. 1.25

    • B. 0.8 (Correct)

    • C. 0.80

    • D. 0

    • E. 1

Question 8: Print Output of Division

  • Code:

int i=4,j=5;
out.print(i/j);
  • Integer Division:

    • Output is 0 since integer division results in flooring the result.

  • Output Choices:

    • A. 23569

Question 9: Printing Entire Alphabet

  • Choices:

    • A. Incorrect

    • B. Correct: for (char let='a'; let<='z'; let++) { out.print(let); }

    • C. Incorrect

    • D. Incorrect

    • E. All of the above (Correct)

Question 10: Conclusion of While Loop

  • Code Segment:

char chr[] = {'r', 'e', 'u', 'n', 'g'};
int m=0,n=1;
while (chr[m] != 'o') { n = m; m++; }
  • Analysis:

    • m will be incremented until it finds 'o', and n stores the index value just before that.

  • Output Choices:

    • A. Correct, n stores the index value of the character 'o'.

    • B. m and n are equal.

    • C. n stores the index value of the character 'a'.

    • D. m stores the index value of the character 'a'.

    • E. n and m both store the index value of the character 'o'.

Question 11: Scanner Object Initialization

  • Code:

System.out.print(scan.nextInt());
scan.close();
  • Choices:

    • A. new Scanner()

    • B. new Scanner(f) (Correct)

    • C. new Scanner("input.dat");

    • D. "input.dat"

    • E. f

Question 12: String Reverse Printing

  • Code:

String sl = "";  
String s2 = "mottthehoople";  
for (int i = s2.length() - 1; i >= 0; i --)  
    sl += s2.charAt(i);
    out.print(sl);
  • Analysis: Given there is an error in i --.

  • Output Choices:

    • A. eoetm

    • B. mteoe

    • C. aloht

    • D. elpoohehtttom

    • E. Throws ArrayIndexOutOfBoundsException.

Question 13: Code Segment Output

  • Code Segment:

int x = 8, y = -5, z = 7;  
x = y = z + 10;  
out.print(x + " " + y + " " + z);  
  • Expected Output: Needs to evaluate correctly.

  • Output Choices:

    • A. 17 17 7

    • B. 20 12 17

    • C. 17 17 17 (Correct)

    • D. 8 -5 17

    • E. 20 17 17

Question 14: Math.max Function Output

  • Code:

out.println(Math.max(Short.SIZE, Byte.MAX_VALUE));
  • Expected Output:

    • A. 32 (Correct)

    • B. 256

    • C. 16

    • D. 128

    • E. 127