Home
Browse
Create
Search
Log in
Sign up
Upgrade to remove ads
Only $2.99/month
CSIT112 Final Exam Review Questions - SP20
STUDY
Flashcards
Learn
Write
Spell
Test
PLAY
Match
Gravity
Terms in this set (44)
Although insertion sort and selection sort have generally the same O(n2) performance, selection sort has an advantage of insertion sort. What is this advantage?
Selection sort usually makes fewer swaps
Which of the following recursive methods would execute approximately log 2 n times for an initial parameter n?
public void logcode(int n)
{
if (n > 1) logcode(n / 2);
}
A Java program can handle an exception in several different ways. Which of the following is not a way that a Java program could handle an exception?
throw the exception to a pre-defined Exception class to be handled
What does the following method compute? Assume the method is called initially with i = 0
public int question9(String a, char b, int i){
if (i = = a.length( )) return 0;
else if (b = = a.charAt(i)) return question9(a, b, i+1) + 1;
else return question9(a, b, i+1);
}
the number of times char b appears in String a
Which of the following messages passed to the String str could throw a StringIndexOutOfBoundsException?
str.charAt(2);
Comparing the performance of selection sort and insertion sort, what can one say?
The efficiencies of both sorts are about the same
The following method should return true if the int parameter is even and either positive or 0, and false otherwise. Which set of code should you use to replace ... so that the method works appropriately?public boolean question3(int x) { ... }
if (x = = 0) return true;else if (x < 0) return false;else return question3(x - 2);
Which of these methods will sort an array of floats into ascending order?
void arrange(float[ ] ary)
{
for (int n=0; n
for (int k=n; k
if (ary[n] > ary[k])
{
float x = ary[n];
ary[n] = ary[k];
ary[k] = x;
}
}
Aside from writing recursive methods, another way that recursion is often used is to define
mathematical functions
For the questions below, assume that
int[ ] a = {6, 2, 4, 6, 2, 1, 6, 2, 6} and consider the recursive method "bar" below.public int bar(int[ ] a, int j){
if (j < a.length)
return a[j] + bar(a, j+1);
else return 0;
}
What is the result of calling bar(a, 0);?
35
If int[ ] x = new int[15]; and the statement x[-1] = 0; is executed, then which of the following Exceptions is thrown?
ArrayIndexOutOfBoundsException
What is the output of the following program?
String msg = " hello ";
msg = 1 + 2 + msg + 3 + 4;
System.out.println(msg);
3 hello 34
Comparing the amount of memory required by selection sort and insertion sort, what can one say?
Neither method requires additional memory
For the questions below, assume values is an int array that is currently filled to capacity, with the following values:
9|2|12|2|6|8|18
The statement System.out.println(values[7]); will
cause an ArrayOutOfBoundsException to be thrown
For the questions below, assume values is an int array that is currently filled to capacity, with the following values:
9|2|12|2|6|8|18
What is returned by values[3]?
2
For the questions below, assume an int array, candy, stores the number of candy bars sold by a group of children where candy[j] is the number of candy bars sold by child j. Assume there are 12 children in all.
Which of the following code could be used to compute the total number of bars sold by the children?
for (int j=0; j<12; j++) sum += candy[j];
For the questions below, use the following partial class definitions:
public class A1{
public int x;
private int y;
protected int z;
...
}
public class A2 extends A1{
protected int a;
private int b;
...
}
public class A3 extends A2{
private int q;
...
}
Which of the following lists of instance data are accessible in A3?
Group of answer choices
x, z, a, q
For the questions below, use the following partial class definitions:
public class A1{
public int x;
private int y;
protected int z;
...
}
public class A2 extends A1{
protected int a;
private int b;
...
}
public class A3 extends A2{
private int q;
...
}
Which of the following is true with respect to A1, A2 and A3?
A3 is a subclass of A2 and A2 is a subclass of A1
For the questions below, use the following partial class definitions:
public class A1{
public int x;
private int y;
protected int z;
...
}
public class A2 extends A1{
protected int a;
private int b;
...
}
public class A3 extends A2{
private int q;
...
}
Which of the following is true regarding the use of instance data y of class A1?
It is accessible only in A1
For the questions below, use the following partial class definitions:
public class A1{
public int x;
private int y;
protected int z;
...
}
public class A2 extends A1{
protected int a;
private int b;
...
}
public class A3 extends A2{
private int q;
...
}
Which of the following lists of instance data are accessible in class A2?
x, z, a, b
An int array stores the following values. Use the array to answer these next questions.
9|2|12|2|6|8|18
How many passes will it take in all for Selection Sort to sort this array?
6
An int array stores the following values. Use the array to answer these next questions.
9|2|12|2|6|8|18
How many passes will it take in all for Insertion Sort to sort this array?
6
An int array stores the following values. Use the array to answer these next questions.
9|2|12|2|6|8|18
Which of the following lists of numbers would accurately show the array after the fourth pass of the Selection Sort algorithm?
2, 4, 6, 8, 12, 9, 18
An int array stores the following values. Use the array to answer these next questions.
9|2|12|2|6|8|18
Which of the following lists of numbers would accurately show the array after the first pass through the Selection Sort algorithm?
2, 4, 12, 9, 6, 8, 18
An int array stores the following values. Use the array to answer these next questions.
9|2|12|2|6|8|18
Which of the following lists of numbers would accurately show the array after the second pass of the Selection Sort algorithm?
Group of answer choices
2, 4, 12, 9, 6, 8, 18
Use the code below to answer the following questions. Note that the catch statements in the code are not implemented, but you will not need those details. Assume filename is a String, x is an int, a is a double array and i is an int. Use the comments i1, i2, i3, e1, e2, e3, e4, e5 to answer the questions (i for instruction, e for exception handler).
try
{
BufferedReader infile = new BufferedReader(new FileReader(filename));// i1
int x = Integer.parseInt(infile.readLine( ));// i2
a[++i] = (double) (1 / x);// i3
}
catch (FileNotFoundException ex) {...}// e1
catch (NumberFormatException ex) {...}// e2
catch (ArithmeticException ex) {...}// e3
catch (ArrayIndexOutOfBounds ex) {...}// e4
catch (IOException ex) {...}// e5
An exception raised by the instruction in i3 would be caught by the catch statement labeled
either e3 or e4
Use the code below to answer the following questions. Note that the catch statements in the code are not implemented, but you will not need those details. Assume filename is a String, x is an int, a is a double array and i is an int. Use the comments i1, i2, i3, e1, e2, e3, e4, e5 to answer the questions (i for instruction, e for exception handler).
try
{
BufferedReader infile = new BufferedReader(new FileReader(filename));// i1
int x = Integer.parseInt(infile.readLine( ));// i2
a[++i] = (double) (1 / x);// i3
}
catch (FileNotFoundException ex) {...}// e1
catch (NumberFormatException ex) {...}// e2
catch (ArithmeticException ex) {...}// e3
catch (ArrayIndexOutOfBounds ex) {...}// e4
catch (IOException ex) {...}// e5
An exception that could also arise in the try statement that does not have an associated catch statement is
NullPointException
Use the code below to answer the following questions. Note that the catch statements in the code are not implemented, but you will not need those details. Assume filename is a String, x is an int, a is a double array and i is an int. Use the comments i1, i2, i3, e1, e2, e3, e4, e5 to answer the questions (i for instruction, e for exception handler).
try
{
BufferedReader infile = new BufferedReader(new FileReader(filename));// i1
int x = Integer.parseInt(infile.readLine( ));// i2
a[++i] = (double) (1 / x);// i3
}
catch (FileNotFoundException ex) {...}// e1
catch (NumberFormatException ex) {...}// e2
catch (ArithmeticException ex) {...}// e3
catch (ArrayIndexOutOfBounds ex) {...}// e4
catch (IOException ex) {...}// e5
An exception raised by the instruction in i2 would be caught by the catch statement labeled
either e2 or e5
Use the code below to answer the following questions. Note that the catch statements in the code are not implemented, but you will not need those details. Assume filename is a String, x is an int, a is a double array and i is an int. Use the comments i1, i2, i3, e1, e2, e3, e4, e5 to answer the questions (i for instruction, e for exception handler).
try
{
BufferedReader infile = new BufferedReader(new FileReader(filename));// i1
int x = Integer.parseInt(infile.readLine( ));// i2
a[++i] = (double) (1 / x);// i3
}
catch (FileNotFoundException ex) {...}// e1
catch (NumberFormatException ex) {...}// e2
catch (ArithmeticException ex) {...}// e3
catch (ArrayIndexOutOfBounds ex) {...}// e4
catch (IOException ex) {...}// e5
An exception raised by the instruction in i1 would be caught by the catch statement labeled
either e1 or e5
For the questions below, use the following skeletal code.
public static void main(String[ ] args){
try
{
ExceptionThrowerCode etc = new ExceptionThrowerCode( );
etc.m1( );
etc.m2( );
}
catch (ArithmeticException ae) { ... }
}public class ExceptionThrowerCode{
...
public void m1( )
{
...
}
public void m2( )
{
try
{
m3( );
}
catch(ArithmeticException ae) {...}
catch(NullPointerException npe) {...}
}
public void m3( )
{
try
{
...
}
catch(ArithmeticException ae) {...}
}
}
If a NullPointerException arises in the try statement in m1
it is not caught leading to the program terminating
For the questions below, use the following skeletal code.
public static void main(String[ ] args){
try
{
ExceptionThrowerCode etc = new ExceptionThrowerCode( );
etc.m1( );
etc.m2( );
}
catch (ArithmeticException ae) { ... }
}public class ExceptionThrowerCode{
...
public void m1( )
{
...
}
public void m2( )
{
try
{
m3( );
}
catch(ArithmeticException ae) {...}
catch(NullPointerException npe) {...}
}
public void m3( )
{
try
{
...
}
catch(ArithmeticException ae) {...}
}
}
If an ArithmeticException arises in the try statement in m3
it is caught in m3
For the questions below, use the following skeletal code.
public static void main(String[ ] args){
try
{
ExceptionThrowerCode etc = new ExceptionThrowerCode( );
etc.m1( );
etc.m2( );
}
catch (ArithmeticException ae) { ... }
}public class ExceptionThrowerCode{
...
public void m1( )
{
...
}
public void m2( )
{
try
{
m3( );
}
catch(ArithmeticException ae) {...}
catch(NullPointerException npe) {...}
}
public void m3( )
{
try
{
...
}
catch(ArithmeticException ae) {...}
}
}
If a NullPointerException arises in the try statement in m3
it is caught in m2
For the questions below, use the following recursive method.
public int question1_2(int x, int y)
{
if (x == y) return 0;
else return question1_2(x-1, y) + 1;
}
If the method is called as question1_2(8, 3), what is returned?
5
For the questions below, use the following recursive method.
public int question1_2(int x, int y){
if (x == y) return 0;
else return question1_2(x-1, y) + 1;
}
Calling this method will result in infinite recursion if which condition below is initially true?
(x < y)
For the questions below, refer to the following recursive factorial method.
public int factorial(int x){
if (x > 1) return x * factorial (x - 1);
else return 1;
}
How many times is the factorial method invoked if originally called with factorial(5)? Include the original method call in your counting.
5
For the questions below, refer to the following recursive factorial method.
public int factorial(int x){
if (x > 1) return x * factorial (x - 1);
else return 1;
}
What is returned if factorial(3) is called?
6
For the questions below, refer to the following recursive factorial method.
public int factorial(int x){
if (x > 1) return x * factorial (x - 1);
else return 1;
}
What condition defines the base case for this method?
(x <= 1)
For the questions below, refer to the following recursive factorial method.
public int factorial(int x){
if (x > 1) return x * factorial (x - 1);
else return 1;
}
What is returned if factorial(0) is called?
1
For the questions below, recall the Towers of Hanoi recursive solution.
If there are 6 disks to move from one Tower to another, how many disk movements would it take to solve the problem using the recursive solution?
63
For the questions below, recall the Towers of Hanoi recursive solution.
If there are 2 disks to move from one Tower to another, how many disk movements would it take to solve the problem using the recursive solution?
3
Assume list is an array of int values, temp is some previously initialized int value, and c is an intinitialized to 0. What does the following code do?
for (j=0; j < list.length; j++)if (list[j] == temp) c++;
Answer: It counts the number of elements in list that are equal to temp
It counts the number of elements in list that are equal to temp
What is the output of executing the following class?
public class Test {public static void main(String[] args) {int x = 5;System.out.print(inc(x) + " " + x);}int inc(int x) {return x+1;}}
Answer: 6 5
6 5
How many * will be printed by the following program?
for (j=0; j<5; j++)for (k=10; k>j; k--)System.out.println("*");
Answer: 40
40
Draw the output of the following program.
for (int j=0; j<3; j++){for (int k=0; k<5; k++)
System.out.print(ʺ*ʺ).;
System.out.println();
}
Answer:
*****
*****
*****
*****
*****
*****
YOU MIGHT ALSO LIKE...
APCS Multiple Choice Exam
56 terms
CompSci
150 terms
AP COMPSCI Abstract
18 terms
Final Exam Studying AP Computer Science
151 terms