KEMBAR78
Guess The Output - Constructors in Java | PDF | Constructor (Object Oriented Programming) | Programming
0% found this document useful (0 votes)
6 views2 pages

Guess The Output - Constructors in Java

The document contains a series of Java code snippets demonstrating the use of constructors in various classes. Each question presents a different scenario involving class instantiation and constructor behavior. The examples illustrate how constructors can be defined and invoked, along with their output when executed.

Uploaded by

premdeoker21
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views2 pages

Guess The Output - Constructors in Java

The document contains a series of Java code snippets demonstrating the use of constructors in various classes. Each question presents a different scenario involving class instantiation and constructor behavior. The examples illustrate how constructors can be defined and invoked, along with their output when executed.

Uploaded by

premdeoker21
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

🔹🔹 Guess the Output – Constructors in Java Q4

Q1 class Data {
class Message { int num;
Message() { Data(int n) {
System.out.println("Constructor called"); num = n;
} }
} }
public class Main { public class Main {
public static void main(String[] args) { public static void main(String[] args) {
Message obj = new Message(); Data d = new Data(100);
} System.out.println(d.num);
} }
------------------------------------------------ }
Q2 ------------------------------------------------
class NumberPrinter { Q5
NumberPrinter(int x) { class Greeting {
System.out.println("Value = " + x); Greeting() {
} System.out.println("Hello from constructor");
} }
public class Main { void show() {
public static void main(String[] args) { System.out.println("Show method");
NumberPrinter np = new NumberPrinter(10); }
} }
} public class Main {
------------------------------------------------ public static void main(String[] args) {
Q3 Greeting g = new Greeting();
class Display { g.show();
Display() { }
System.out.println("Default"); }
} ------------------------------------------------
Display(int x) { Q6
System.out.println("Parameterized: " + x); class MessagePrinter {
} MessagePrinter() {
} System.out.println("No-arg");
public class Main { }
public static void main(String[] args) { MessagePrinter(String msg) {
Display d1 = new Display(); System.out.println("Message: " + msg);
Display d2 = new Display(50); }
} }
} public class Main {
------------------------------------------------ public static void main(String[] args) {
MessagePrinter mp = new
MessagePrinter("Java");
}
}
------------------------------------------------
Q7 Q10
class Logger { class Example {
Logger() { Example() {
System.out.println("Constructor"); System.out.println("Constructor 1");
} }
} Example(int x) {
public class Main { System.out.println("Constructor 2 with value " +
public static void main(String[] args) { x);
new Logger(); }
new Logger(); }
} public class Main {
} public static void main(String[] args) {
------------------------------------------------ Example e1 = new Example();
Q8 Example e2 = new Example(99);
class ObjectCreator { }
ObjectCreator() { }
System.out.println("Object created");
}
}
public class Main {
public static void main(String[] args) {
ObjectCreator o1 = new ObjectCreator();
ObjectCreator o2 = o1;
}
}
------------------------------------------------
Q9
class Calculator {
int val;
Calculator(int v) {
val = v;
}
}
public class Main {
public static void main(String[] args) {
Calculator c1 = new Calculator(10);
Calculator c2 = new Calculator(20);
System.out.println(c1.val + c2.val);
}
}
------------------------------------------------

You might also like