cbquiz1

Corrections & Tricky Questions

Question 15

Consider the following method, which is intended to return true if at least one of the three strings s1, s2, or s3 contains the substring "art". Otherwise, the method should return false.

Adding strings (index)

String all = s1 + s2 + s3;
//adding strings, each letter has its own index
//checking for "art" - will be checking 3 letter as a group at a time, if there is "art", then it will return the index of the first letter in the group
return(all.indexof("art") != -1);
//return true if index is not -1
//if there is not "art" in the string, the index will be -1, which will return false

If all.indexof("art") != -1 evaulates to true, but "art" is from 2 strings, then it is not working as intended because it should only return true if "art" is existed within the same string.

Question 16

Confusing wording (understand what the question is asking)

Enhanced for loop

for(int[] row : col) //the right side should be the name of array, not col

for(int[] row : arr)

Question 24

2d array vs array

for (int row = 0; row <= arr.length; row++)
// if it is an array, then it will not work because it should be row < arr.length
// but since it is an 2d array, it will create a new element that is all 0s
// therefore all of the options would return true

Since all of the answer options would return true becuase they all contain 0, but the one that contains no "0" would also evaluates to true when it is supposed to evaluates to false. 10, 20, 30... would not count because it is not "0" althought it does has 0 inside

Question 25

class, constuctor method, mutator method, tester method

constructor method - same name as class name, don't need return type (must have constructor in a class) mutator method - need return type tester method - public static void..., no return type, so can use System.out.println to test

public updateItems(int newNum)

{

numItems = newNum;

}
//this is a mutator method
//so it needs a return type in front of the name and "return" infront of the variable name that we want to return

Question 27

there are 2 j++!!!

Question 28

the j++ skips the second "3" (low score that we want to remove)

Question 42

.substring()

str = candy;
str.substring(1,4); //output: and (not included index 4)

Question 54

else if (str.substring(0, 1).compareTo(str.substring(1, 2)) > 0) //compareTo(), compare two substrings/letters

The compareTo() method compares two strings lexicographically.

The method returns 0 if the string is equal to the other string. A value less than 0 is returned if the string is less than the other string (less characters) and a value greater than 0 if the string is greater than the other string (more characters).

Question 64

int[] arr = {1, 2, 4, 0, 3};

for (int i : arr)

{

System.out.print(i); //not arr[i] because i is not representing the index in for each loop

}
int[] arr = {1, 2, 4, 0, 3};

for (int i = 0; i < arr.length; i++)

{

System.out.print(arr[i]); //not i because it would just return the index number

}

Responses