Computer Science › Standard Operations & Algorithms
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.
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.
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.
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
return parse(this,parseOrder);
}
private static ArrayList
ArrayList
if(node == null) {
return(retVal);
}
ArrayList
ArrayList
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
for(String s : traversedNames) {
System.out.println(s);
}
}
What is the output for this method?
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
return parse(this,parseOrder);
}
private static ArrayList
ArrayList
if(node == null) {
return(retVal);
}
ArrayList
ArrayList
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
for(String s : traversedNames) {
System.out.println(s);
}
}
What is the output for this method?
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
return parse(this,parseOrder);
}
private static ArrayList
ArrayList
if(node == null) {
return(retVal);
}
ArrayList
ArrayList
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
for(String s : traversedNames) {
System.out.println(s);
}
}
What is the output for this method?
Which of the following implements a method named contains
for searching an array sequentially, confirming whether or not the array contains a requested element?
Which of the following implements a method named contains
for searching an array sequentially, confirming whether or not the array contains a requested element?
Which of the following implements a method named contains
for searching an array sequentially, confirming whether or not the array contains a requested element?
{1, 9, 5, 4, 2, 0, 4}
What would the set of numbers look like after four iterations of Insertion Sort?