Computer Science Invitational Test S19A Notes
Computer Science Invitational Test S19A
Question 1: Binary Sum Calculation
Determine the sum of the binary numbers 11100111₂ and 10110001₂.
Choices:
A. 40810
B. 6318
C. 110011001₂
Question 2: Output of Code Snippet
Evaluate the output of the following line of code:
out.print (14/2.0+3);
Choices:
A. 19716
B. 10
C. 2
D. 2.0
E. 10.0
F. None of the above
G. 19
Question 3: Printed Output of Code Segment
The following code outputs:
out.println("Houston");
out.print ("San \nAntonio");
Choices:
A. Houston
B. Houston San
C. Houston San Antonio
D. Houston San Antonio
E. Error. Will not compile due to an invalid escape sequence.
Question 4: Substring Usage
Analyze the code:
String s1="Dallas", s2="El Paso";
String s3=s2.substring(0, 2)+s1+s2.substring(3);
out.print(s3);
Output Choices:
A. Dallas El Paso
B. El PDallasaso
C. El Dallas Paso
D. DallasEl Paso
E. ElDallas Paso
Question 5: Boolean Expression Output
Examine the following boolean expression:
boolean bl=true, b2=false, b3=false;
out.println(!bl&&b2&&!b3);
Output Choices:
A. true
B. false
Question 6: Minimum Value Calculation
Consider the code:
double d1=8.36;
double d2=-15.01;
out.print (Math.min(d1, d2));
Output Choices:
A. -15.01
B. 8.36
C. -15.01 8.36
D. 8.36 -15.01
E. 8
Question 7: Division Output
Evaluate:
double d=4;
int i=8;
out.print(i/d);
Output Choices:
A. 0
B. 2.0
C. 2
D. 4.0
E. 12
Question 8: String Manipulation
Analyze the following code:
String str="Lollapalooza";
if(str.length() <=11)
out.print("A");
if (str.endsWith("ooza"))
out.print("B");
else if(str.charAt (1)=='0')
out.print("C");
Output Choices:
A. A
B. B
C. ABCD
D. C
E. AB
F. AC
Question 9: Loop Output
Examine the loop:
int x=6;
while(x>0) {
if (x==2)
out.print ("\n");
out.print("#");
x--;
}
Output Choices:
A. ###
B. ##
C. ####
D. ######
E. ####
Question 10: Array Modification Output
Analyze the following array operations:
String[] list= {"public", "class", "extends", "void", "int", "double"};
list [2]="boolean";
list [list.length-3]="static";
out.print (Arrays.toString(list));
Output Choices:
A. [public, class, boolean, extends, static, void, int, double]
B. [public, boolean, extends, void, static, double]
C. [public, class, extends, boolean, void, static, int, double]
D. [public, class, boolean, static, int, double]
E. [public, class, int, double, boolean, static]
Question 11: Error Identification in Code
Identify the line containing an error:
public class Q11 {
public static void main(String[] args) throws IOException {
Scanner scan=new Scanner(new File("input.dat"));
int sum=0, count=0;
while (scan.hasNextInt()) {
sum+=scan.nextInt();
count++;
}
out.print(count+" "+sum);
}
}
Output Choices:
A. line #1
B. line #2
C. line #3
D. line #4
E. None of the above. There are no errors in the code.
Question 12: Code Output Calculation
Analyze:
long x=5;
for (int y=3;y<15;y+=3)
x+=y;
out.print(x);
Output Choices:
A. 45
B. 30
C. 35
D. 50
E. 32
Question 13: Operator Precedence
Identify which operator is applied last:
Choices:
A. &&
B. ++
C. !
D. <=
Question 14: Short Data Type Limits
Determine which value cannot be stored in a short variable:
Choices:
A. -32768
B. -32767
C. 32768
D. 32767
E. None of the above. All values are valid.
Question 15: ArrayList Size Output
Evaluate:
ArrayList<Double> a=new ArrayList<Double>();
a.add(5.25); a.add(6.5); a.add(3.99);
a.add(4.75); a.add(1.0); a.add(0.5);
out.print (a.size());
Output Choices:
A. 5
B. 6
C. 4
D. 3
E. 0
Question 16: Code Segment Modification
Determine which can replace <code> in the class Pet:
public class Pet {
private String species;
private String name;
private double weight;
public Pet (String s, String n, double w) {
species = s;
name = n;
weight = W;
}
}
Choices:
A. implements
B. extends
C. super
D. this
E. double
Question 17: Super Constructor Call
Which constructor is called by super() in the first constructor?
Choices:
A. Util
B. String
C. System
D. Pet
E. Object
Question 18: Constructor Characteristics
The constructors in the Pet class are characterized as:
Choices:
A. overloaded
B. overridden
C. overwritten
D. parallel
Question 19: Output of Pet Class Code
What is the output of the following client code?
Pet pet=new Pet("cat", "Felix");
out.println(pet);
Output Choices:
A. Felix cat null
B. cat Felix
C. Felix cat
D. Felix cat 0.0
E. There is no output due to an error.
Question 20: String Length Calculation
What does the following code output?
String s="fosterthepeople";
String[] sa=s.split("[eo]");
out.println(sa.length);
Output Choices:
A. 5
B. 4
C. 2
D. 1
E. 0
Question 21: Nested Array Output
Evaluate:
int[][] a = new int[3][];
int[] x = {1, 3, 5, 7};
int[] y = {8, 6, 4};
int[] z = {2, 3};
a[2]=x;
a[0]=y;
a[1]=z;
out.println(a[a[1][0]][a[2][1]]);
Output Choices:
A. 7
B. 6
C. 10
D. There is no output due to an error.
Question 22: Method Return Value
Identify the output of the client code:
out.print(mtd(3, 6));
public static int mtd(int x, int n) {
if (x==-n)
return x;
else
return x+mtd(++x,n);
}
Output Choices:
A. 25
B. 20
C. 18
D. 15
E. 12
Question 23: HashSet Size Calculation
Determine the output of the following code:
Set<String> hs = new HashSet<String>();
hs.add("Dalhart");
hs.add("Borger");
hs.add("Silverton");
hs.add("Bovina");
hs.add("Borger");
hs.remove("Silverton");
out.print(hs.size());
Output Choices:
A. 3
B. 4
C. 5
D. 2
E. 0
Question 24: Scanner with Delimiters
Analyze the following code:
Scanner s = new Scanner("googlefacebookamazon");
s.useDelimiter("[aeiou]+);
int x = 0;
while (s.hasNext()) {
s.next();
x++;
}
out.print(x);
Output Choices:
A. 9
B. 10
C. 7
D. 1
E. 0
Question 25: Regular Expression Matcher
Evaluate the output of:
Pattern p = Pattern.compile("\\w?");
Matcher m = p.matcher("1");
out.print(m.matches());
Output Choices:
A. 0
B. 1
C. 49
D. true
E. false
Question 26: Iterator for List
What must replace <code> in the following code?
Iterator<String> i=<code>;
out.print(i.next());
Options:
A. iterator();
B. iterator(list);
C. Iterator.iterator();
D. new Iterator();
E. list.iterator();
Question 27: Output from Iterator Code
If <code> is correctly filled, what is the result of:
List<String> list = new LinkedList<String>();
list.add("list");
list.add("set");
out.print(i.next());
Output Choices:
A. list
B. set
C. map
D. collection
E. iterator
Question 28: Worst-case Time Complexity
What is the worst-case run time efficiency of the following code?
int n=10000;
while (n>0) {
n/=2;
out.println(n);
}
Time Complexity Choices:
A. O(log n)
B. O(n log n)
C. O(n²)
D. O(n)
E. O(n³)
Question 29: String Comparison Output
What does this line print?
out.print("recursion".compareTo("recovery"));
Output Choices:
A. 567
B. 0
C. -1
D. 1
E. -1
Question 30: Random Number Generation Output
Which of the following may not be printed by the following code?
Random r = new Random();
out.print(r.nextInt(50));
Output Choices:
A. 0
B. 1
C. 50
D. 51
E. More than one of the above.
Question 31: Method Implementation
Which of the following must replace <code> in the search method?
public static int search (String[] list, String t) {
for (int i=0;i<list.length; i++) {
if (t.equals(list[i]))
return <code>;
}
return -1;
}
Options:
A. list[i]
B. list[i].length()
C. list.length
D. i
E. t
Question 32: Type of Search Implemented
Assuming <code> is filled out correctly, which type of search does the search method implement?
Choices:
A. heap
B. recursive
C. binary
D. sequential
E. selection
Question 33: Time Complexity of Search Method
What is the worst-case run time efficiency of the search method?
Efficiency Choices:
A. O(1)
B. O(n²)
C. O(n)
D. O(log n)
E. O(n log n)
Question 34: String Conditional Output
Evaluate the following:
int moon=1;
String str="hello"+(moon>1?" goodbye " +moon+" yes ":" no "+moon+");
out.print(str);
Output Choices:
A. hello no 1 maybe
B. hello goodbye 1 yes
C. no 1 maybe
D. goodbye 1 yes
E. hello no maybe
Question 35: Replacement in Code Segment
Which cannot replace <code> in the given ArrayList segment?
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(1); list.add(2);
for(<code> i:list)
out.println(i);
Choices:
A. int
B. Integer
C. double
D. ArrayList
E. More than one of the above.
Question 36: Data Structure Characteristics
Which of the following is true about the data structure diagrammed?
Choices:
A. It is a binary search tree.
B. It is a max heap.
C. It is a complete tree.
D. It is a min heap.
E. It is a complete graph.
Question 37: Boolean Expression Equivalence
Which Boolean expression is equivalent to the diagram?
Choices:
A. Ã * B⇒C
B. A * B C
C. A + B * C
D. A + C + B
E. A * B + C
Question 38: Graph Properties
Characterize the graph shown:
Choices:
A. undirected, weighted and complete
B. directed, weighted and complete
C. directed and unweighted
D. connected, simple and complete
E. directed and weighted
Question 39: Two's Complement Conversion
Write the binary 8-bit two's complement equivalent of 99 in the answer document.
Question 40: Infix Expression Conversion
Write the infix equivalent of the postfix expression AB+ CD * E/- in the answer document.