Question: What is the output of the following code? Java C# class Counter { private int seconds = 0 ; public void addOne ( ) {

What is the output of the following code?
Java C#
class Counter {
private int seconds=0;
public void addOne(){
seconds++;
}
public void subtractOne(){
seconds--;
}
public int currentCount(){
return seconds;
}
}
class Main {
public static int doStuff(int x, Counter a){
x++;
a.addOne();
return x+a.currentCount();
}
public static void main(String[] args){
int x=0;
int y=0;
Counter a = new Counter();
a.addOne();
y=doStuff(x,a);
System.out.println("a "+a.currentCount()+" x "+x+" y "+y);
}
}
using System;
class Counter {
private int seconds=0;
public void addOne(){
seconds++;
}
public void subtractOne(){
seconds--;
}
public int currentCount(){
return seconds;
}
}
class MainClass {
public static int doStuff(int x, Counter a){
x++;
a.addOne();
return x+a.currentCount();
}
public static void Main (string[] args){
int x=0;
int y=0;
Counter a = new Counter();
a.addOne();
y=doStuff(x,a);
Console.WriteLine("a "+a.currentCount()+" x "+x+" y "+y);
}
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Finance Questions!