How many times will the following loop execute? What will be the final
output?
int z = l;
while(z < 10)
if(z++ % 2 == 0)
System.out.println(z);
Answers
Answer:
class q1a{
public static void sumUp(int n, int total){
int i;
for (i=1; i<=n; i++){
total = total +i;
}
}
public static void main(String args[]){
int n, total;
n=10; //n can be initialized to any +ve number
total=0;
sumUp(n, total);
System.out.println(total);
Explanation:
Reason: Method sumUp does not return any
value. The change in the local variable total is
not visible outside the method. Reason:
- Initially an object is created which is referred by q
and has instance variables p=10 and q=66.
- When method origin1() is invoked the parameter p
starts referring to the object referred by q. However,
p=new point(0,0) creates a new object (instance
variables p=0, q=0), and p starts referring to the new
object. Since, p is a local variable, any change to p is
not visible outside the method. Therefore, instance
variables of q do not change
- q.printpoint(), therefore, prints values of instance
variables of q which are 10 and 66
- method origin2() is invoked and parameter p starts
referring to object referred by q. Through use of p
instance variables of object referred by q, are
modified. The change is done in the object itself.
Hence, instance variables of the object change to p=15
and q=20.
- q.printpoint() prints values of instance variables of q
which are now 15 and 20.
mark me brainliest