Standard Operations & Algorithms

Practice Questions

Computer Science › Standard Operations & Algorithms

Page 1 of 9
10 of 87
1

public static int[] doWork(int[] arr, int val,int index) {

int[] ret = new int[arr.length + 1];

for(int i = 0; i < index; i++) {

ret[i] = arr[i];

}

ret[index] = val;

for(int i = index + 1; i < ret.length; i++) {

ret[i] = arr[i - 1];

}

return ret;

}

Which of the following is a possible error in the first loop in code above?

I. The array arr might be indexed out of bounds.

II. The array ret might be indexed out of bounds.

III. A null pointer exception might occur.

2

public static int[] doWork(int[] arr, int val,int index) {

int[] ret = new int[arr.length + 1];

for(int i = 0; i < index; i++) {

ret[i] = arr[i];

}

ret[index] = val;

for(int i = index + 1; i < ret.length; i++) {

ret[i] = arr[i - 1];

}

return ret;

}

Which of the following is a possible error in the first loop in code above?

I. The array arr might be indexed out of bounds.

II. The array ret might be indexed out of bounds.

III. A null pointer exception might occur.

3

public static int[] doWork(int[] arr, int val,int index) {

int[] ret = new int[arr.length + 1];

for(int i = 0; i < index; i++) {

ret[i] = arr[i];

}

ret[index] = val;

for(int i = index + 1; i < ret.length; i++) {

ret[i] = arr[i - 1];

}

return ret;

}

Which of the following is a possible error in the first loop in code above?

I. The array arr might be indexed out of bounds.

II. The array ret might be indexed out of bounds.

III. A null pointer exception might occur.

4

Consider the code below:

public static class BTNode {

public static final int PARSE_IN = 1;

public static final int PARSE_PRE = 2;

public static final int PARSE_POST = 3;

String name;

BTNode lPointer,rPointer;

public BTNode(String s) {

name = s;

lPointer = rPointer = null;

}

public void insert(String s) {

insert(this,s);

}

private static void insert(BTNode node,String s) {

int comparison = s.compareTo(node.name);

if(comparison < 0) {

if(node.lPointer != null) {

insert(node.lPointer,s);

} else {

node.lPointer = new BTNode(s);

}

} else if(comparison > 0) {

if(node.rPointer != null) {

insert(node.rPointer,s);

} else {

node.rPointer = new BTNode(s);

}

}

}

public ArrayList parse(final int parseOrder) {

return parse(this,parseOrder);

}

private static ArrayList parse(BTNode node, final int parseOrder) {

ArrayList retVal = new ArrayList();

if(node == null) {

return(retVal);

}

ArrayList leftList = parse(node.lPointer,parseOrder);

ArrayList rightList = parse(node.rPointer,parseOrder);

if(parseOrder == PARSE_PRE) {

retVal.add(node.name);

retVal.addAll(leftList);

retVal.addAll(rightList);

} else if (parseOrder == PARSE_POST) {

retVal.addAll(leftList);

retVal.addAll(rightList);

retVal.add(node.name);

} else {

retVal.addAll(leftList);

retVal.add(node.name);

retVal.addAll(rightList);

}

return retVal;

}

}

public static void main(String\[\] args) {

String\[\] names = {"Hervaeus","Peter Auriol","Guiral","Felix","Lila","Lola","Yippy","Yiiiipppy","Acton","Pierce","Betty"};

BTNode node = new BTNode(names\[0\]);

for(int i = 1; i < names.length; i++) {

node.insert(names\[i\]);

}

ArrayList traversedNames = node.parse(BTNode.PARSE_IN);

for(String s : traversedNames) {

System.out.println(s);

}

}

What is the output for this method?

5

Consider the code below:

public static class BTNode {

public static final int PARSE_IN = 1;

public static final int PARSE_PRE = 2;

public static final int PARSE_POST = 3;

String name;

BTNode lPointer,rPointer;

public BTNode(String s) {

name = s;

lPointer = rPointer = null;

}

public void insert(String s) {

insert(this,s);

}

private static void insert(BTNode node,String s) {

int comparison = s.compareTo(node.name);

if(comparison < 0) {

if(node.lPointer != null) {

insert(node.lPointer,s);

} else {

node.lPointer = new BTNode(s);

}

} else if(comparison > 0) {

if(node.rPointer != null) {

insert(node.rPointer,s);

} else {

node.rPointer = new BTNode(s);

}

}

}

public ArrayList parse(final int parseOrder) {

return parse(this,parseOrder);

}

private static ArrayList parse(BTNode node, final int parseOrder) {

ArrayList retVal = new ArrayList();

if(node == null) {

return(retVal);

}

ArrayList leftList = parse(node.lPointer,parseOrder);

ArrayList rightList = parse(node.rPointer,parseOrder);

if(parseOrder == PARSE_PRE) {

retVal.add(node.name);

retVal.addAll(leftList);

retVal.addAll(rightList);

} else if (parseOrder == PARSE_POST) {

retVal.addAll(leftList);

retVal.addAll(rightList);

retVal.add(node.name);

} else {

retVal.addAll(leftList);

retVal.add(node.name);

retVal.addAll(rightList);

}

return retVal;

}

}

public static void main(String\[\] args) {

String\[\] names = {"Hervaeus","Peter Auriol","Guiral","Felix","Lila","Lola","Yippy","Yiiiipppy","Acton","Pierce","Betty"};

BTNode node = new BTNode(names\[0\]);

for(int i = 1; i < names.length; i++) {

node.insert(names\[i\]);

}

ArrayList traversedNames = node.parse(BTNode.PARSE_IN);

for(String s : traversedNames) {

System.out.println(s);

}

}

What is the output for this method?

6

Consider the code below:

public static class BTNode {

public static final int PARSE_IN = 1;

public static final int PARSE_PRE = 2;

public static final int PARSE_POST = 3;

String name;

BTNode lPointer,rPointer;

public BTNode(String s) {

name = s;

lPointer = rPointer = null;

}

public void insert(String s) {

insert(this,s);

}

private static void insert(BTNode node,String s) {

int comparison = s.compareTo(node.name);

if(comparison < 0) {

if(node.lPointer != null) {

insert(node.lPointer,s);

} else {

node.lPointer = new BTNode(s);

}

} else if(comparison > 0) {

if(node.rPointer != null) {

insert(node.rPointer,s);

} else {

node.rPointer = new BTNode(s);

}

}

}

public ArrayList parse(final int parseOrder) {

return parse(this,parseOrder);

}

private static ArrayList parse(BTNode node, final int parseOrder) {

ArrayList retVal = new ArrayList();

if(node == null) {

return(retVal);

}

ArrayList leftList = parse(node.lPointer,parseOrder);

ArrayList rightList = parse(node.rPointer,parseOrder);

if(parseOrder == PARSE_PRE) {

retVal.add(node.name);

retVal.addAll(leftList);

retVal.addAll(rightList);

} else if (parseOrder == PARSE_POST) {

retVal.addAll(leftList);

retVal.addAll(rightList);

retVal.add(node.name);

} else {

retVal.addAll(leftList);

retVal.add(node.name);

retVal.addAll(rightList);

}

return retVal;

}

}

public static void main(String\[\] args) {

String\[\] names = {"Hervaeus","Peter Auriol","Guiral","Felix","Lila","Lola","Yippy","Yiiiipppy","Acton","Pierce","Betty"};

BTNode node = new BTNode(names\[0\]);

for(int i = 1; i < names.length; i++) {

node.insert(names\[i\]);

}

ArrayList traversedNames = node.parse(BTNode.PARSE_IN);

for(String s : traversedNames) {

System.out.println(s);

}

}

What is the output for this method?

7

Which of the following implements a method named contains for searching an array sequentially, confirming whether or not the array contains a requested element?

8

Which of the following implements a method named contains for searching an array sequentially, confirming whether or not the array contains a requested element?

9

Which of the following implements a method named contains for searching an array sequentially, confirming whether or not the array contains a requested element?

10

{1, 9, 5, 4, 2, 0, 4}

What would the set of numbers look like after four iterations of Insertion Sort?

Page 1 of 9
Return to subject