17.. class bank
{
double bal=2000.0; String s="Anu";
String accout_type="SB";
public bank()
{
System.out.println(s + "\t"+ bal);
}
public static void main()
{
bank obj = new bank();
}
What is the output of the given program?
A) Error
B) Anu SB 2000
C) Anu SB 2000
D) Anu 2000.0
Answers
Answered by
11
Answer:
Option D: Anu 2000.0
Códe :-
class bank
{
double bal=2000.0; String s="Anu";
String accout_type="SB";
public bank()
{
System.out.println(s + "\t"+ bal);
}
public static void main()
{
bank obj = new bank();
}
}
When the above códe runs, control will first execute main(). In main() an object of a class is being created using new operator and at the same time, a constructor (bank()) is being called. So the control will now execute the constructor. In constructor, value of s which is "Anu" and value of bal which is 2000.0 is printed. The control then returns to main() where the program finally terminates.
Extra:-
- In the print statement, \t escape sequence is present which is used to insert a tab in terminal window. So the output will be,
- Constructor is always called during object creation.
- Constructor name is same as the class name.
- Escape Sequences are always given in double quotes
Similar questions