#1 Analyze the following code. double sum = 0; double d; for (d = 0; d < 10; d =
ID: 3570094 • Letter: #
Question
#1 Analyze the following code.
double sum = 0;
double d;
for (d = 0; d < 10; d = d + 0.1 )
{
sum = sum + d;
}
a.The program has a syntax error because the semicolons in the for loop are in the wrong place.
#2 What is y after the following for loop statement is executed?
int y = 0;
for (int i=0; i < 10; i ++)
{
y ++;
}
a. 9
#3 Array arr is declared and initialized in the following line of code:
int [ ] arr = {10, 7, 3, 5, 9};
What is arr[3] - arr[1]?
#4 Which of the following is a valid array declaration and initialization?
a. char [ ] charArray;
charArray = new char[26];
b. int [ ] words;
words = new words[10];
c. char [ ] charArray;
charArray = 15;
d. double [3] nums;
#5 Consider the following code fragment:
int [ ] list;
list = new int[10];
for (int i = 0; i <= 10; i = i + 1)
{
list[i] = 0;
}
Which of the following statements is true?
a.The loop body will execute 9 times
#6 Assume the declaration of the method xMethod is as follows. This means that xMethod is sent one object that is an array of double values.
public static void xMethod(double [ ] a);
Which of the following could be used to call xMethod?
a. xMethod(5.7);
b. xMethod(
b.The program has a syntax error because the control variable in the for loop cannot be double.Explanation / Answer
double sum = 0;
double d;
for (d = 0; d < 10; d = d + 0.1 )
{
sum = sum + d;
}
a.The program has a syntax error because the semicolons in the for loop are in the wrong place.
b.The program has a syntax error because the control variable in the for loop cannot be double.
c. The program compiles but does not stop because d would always be less than 10.
d. The program compiles and runs fine.
#2 What is y after the following for loop statement is executed?
int y = 0;
for (int i=0; i < 10; i ++)
{
y ++;
}
a. 9
b. 10
c. 11
d. 12
#3 Array arr is declared and initialized in the following line of code:
int [ ] arr = {10, 7, 3, 5, 9};
What is arr[3] - arr[1]?
a. -7
b. 7
c.-2
d. 2
#4 Which of the following is a valid array declaration and initialization?
a. char [ ] charArray;
charArray = new char[26];
b. int [ ] words;
words = new words[10];
c. char [ ] charArray;
charArray = 15;
d. double [3] nums;
#5 Consider the following code fragment:
int [ ] list;
list = new int[10];
for (int i = 0; i <= 10; i = i + 1)
{
list[i] = 0;
}
Which of the following statements is true?
a.The loop body will execute 9 times
b. The program has a syntax error because the control variable, i, in the for loop is not defined in the body of the loop.
c.The loop body will execute 10 times, filling up the array with zeros.
d. The code has a runtime error indicating that the array is out of bound.
#6 Assume the declaration of the method xMethod is as follows. This means that xMethod is sent one object that is an array of double values.
public static void xMethod(double [ ] a);
Which of the following could be used to call xMethod?
a. xMethod(5.7);
b. xMethod(
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.