Answer the Questions(Multiple Choice ): Question 11 What are the values of i and
ID: 3860724 • Letter: A
Question
Answer the Questions(Multiple Choice):
Question 11
What are the values of i and sum after this code sequence is executed?
int i = 0;
int sum = 0;
for ( i = 0; i < = 40; i++)
{
if ( i % 10 = = 0)
sum += i ;
}
i is 40
sum is 60
i is 41
sum is 60
i is 40
sum is 100
i is 41
sum is 100
2.5 points
Question 12
What is the output of the following code sequence:
double a = 16 / 5;
System.out.println( a );
2.6
3.2
a
3.0
2.5 points
Question 13
Given code:
public abstract class A{ }
public class B extends A
{
}
The following code sequence would correctly create an object reference of class A holding an object reference for an object of class B:
A c;
c = new B();
True
False
2.5 points
Question 14
Given the following code declaring and initializing two int variables x and y with respective values 7 and 9, answer question 14 indicating whether the value of the expression is true or false.
int x = 7;
int y = 9;
Expression: ( y – x == 0 || ( x + 2 ) != y)
True
False
2.5 points
Question 15
Class SimpleDate encapsulates integer instance variables month, day and year. Consider the following equals method in class SimpleDate:
public boolean equals( Object o )
{
if ( !( d instanceof SimpleDate ) )
return false;
( Insert appropriate code from selections below)
if ( month == d1.month
&& day == d1.day
&& year == d1.year )
return true;
else
return false;
}
Which statement(s) could be used to complete the above method body code correctly?
SimpleDate d1;
d1 = (SimpleDate)o;
SimpleDate d1 = o;
SimpleDate d1 = (SimpleDate)o;
Both a and c
a.i is 40
sum is 60
i is 41
sum is 60
i is 40
sum is 100
i is 41
sum is 100
Explanation / Answer
11.
d) i is 41 sum is 100.
loop runs for 41 times and sum is sum of 10+20+30+40 = 100
12.
d) 3.0
as both 16 and 5 are ints, there will be precision lost.
13. True.
This is an example of inheritence. An abstract class cannot be instantiated, so we cannot create object of class A using A a = new A(). Instead we can create it using the child class i.e. A a = new B();
14. False
because y - x == 0 is False as 7 - 9 = -2 and ( x + 2 ) != y is also False because x+2 is 9 and that is equal to 9
15. d) Both a and c are correct type of object declaration