Question 3

(c) State whether any error exists in the following code. If so, correct the error and
give output.

class Test {
public static void main(String args[]) {
A a = new A();
a.print();
}
}
class A {
String s;
A(String s) {
this.s=s;
}
public void print() {
System.out.println(s);
}
}
class Test 
{
public static void main(String args[])
{
A a = new A("Jaydip");
a.print();
}
}
class A
{
String s;
A(String s)
{
this.s=s;
}
public void print()
{
System.out.println(s);
}
}
(c) Give output of the following program:

public class Test {
public static void main(String args[]) {
Count myCount = new Count();
int times=0;
for(int i=0;i<100;i++)
increment(myCount,times);
System.out.println("count is "+myCount.count);
System.out.println("times is "+times);
}
public static void increment(Count c,int times) {
c.count++;
times++;
}
}
class Count {
public int count;
Count(int c){ count=c; }
Count(){ count=1; }
}
//count is 101
//time is 0
public class Test
{
public static void main(String args[])
{
Count myCount = new Count();
int times=0;
for(int i=0;i<100;i++)
increment(myCount,times);
System.out.println("count is "+myCount.count);
System.out.println("times is "+times);
}
public static void increment(Count c,int times)
{
c.count++;
times++;
}
}
class Count
{
public int count;
Count(int c)
{
count=c;
}
Count()
{
count=1;
}
}

Make Comments..!!


Oops!! No posts from user.

Download Android App