This is not homework, but I am reviewing for a final and would like a better und
ID: 3582138 • Letter: T
Question
This is not homework, but I am reviewing for a final and would like a better understanding of these problems that I do not fully understand. Any help would be appreciated
Problem 1: Fill in the Blank
Write the result of each evaluated expression in the provided blank. Assume the following declarations and initializations. If you believe the expression will result in a compile time (syntax) error or runtime error (exception) write “compile time error” or “runtime error” respectively.
String s = “hello world”; String t = “”;
int[] arr = {1,10,3,11,7};
double[] drr = new double[100];
Problem 4: Read some code (Explanations for this part would be helpful)
Read the code below and answer the questions that follow.
char[] arr = {‘t’, ‘e’, ‘s’, ‘t’, ‘i’, ‘t’}; String p = “CS111 is the best!”;
//loop A
for(int i=0; i< p.length(); i++){
char c = p.charAt(i);
//loop B
for(int j=0; j < arr.length; j++){
if(arr[j] == c){ arr[j] = ‘X’; break;
}
}
}
2. How many times does j get incremented (total)? _
3. How many times does the break statement run? _
6. Remove the break; statement
a. How many times does j get incremented (total) _
Explanation / Answer
a)s.length
"compile time error"
it should be s.length()
length() is a method for String s=data types
b)arr.length
5 return length of array
here length is a attribute for array elements not a method
c)3
returns element at index 2
d)11
returns length of string
e)l
returns character at index 3 in String
f)100
returns length of array
g)0
returns length of String t
h)HELLO WORLD
converts s to UpperCase and returns it
i)4
returns index of first occurrence of string "o"
j)7
returns index of first occurrence of string "o" from index 6
k)e
returns substring including index 1 and excluding index 2
l)world
s.indexOf("wo") returns index of substring "wo"
s.substring(index) reurns substring starting from index
s.substring(s.indexOf("wo"))
m)2
s.indexOf(s.substring(2,5))
returns substring starting from 2 (including) to 5(excluding)
s.IndexOf(substring) returns index of first occurence of substring
n)orld
s.substring(arr[4])
arr[4] returns 7
s.substring(7) returns substring starting from index 7
o)11
arr[arr[2]]
arr[2] returns 3
arr[3] returns 11
prob b)
1)18
i is incremented length of String p times
length of String p is 18
2)88 times
it should be 18*6 but beacuse of break statement inner for loop some times end in between
3)5 times
4)CS111 is the best!
p value doest change
5)XXXXXt
6)108 times
j increments 6 times in inner loop for each i
and i crements 18 times
so 18*6
b)XXXXXX
7) for each character in p it is trabersing the array arr
if charcter in p matches character in arr replacing character in arr with 'X'
and breaking the inner for loop
in simpler way for each character in p replacing the first occurence of it in arr with X
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.