0% found this document useful (0 votes)
21 views

Quiz About Java Constructors

Uploaded by

mostafa nasser
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Quiz About Java Constructors

Uploaded by

mostafa nasser
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Java Constructors

Question 1

Is there any compiler error in the below Java program?

Java

class Point {
int m_x, m_y;
public Point(int x, int y) { m_x = x; m_y = y; }
public static void main(String args[])
{
Point p = new Point();
}
}

Yes

No

Discuss it

Question 1 ‒ Explanation
The main function calls parameterless constructor, but there is only one constructor defined in class which takes two
parameters. Note that if we write our own constructor, then compiler doesn\'t create default constructor in Java. Thi
s behavior is same as C++.

Question 2

JAVA

class Test
{
static int a;

static
{
a = 4;
System.out.println ("inside static block\\n");
System.out.println ("a = " + a);
}

Test()
{
System.out.println ("\\ninside constructor\\n");
a = 10;
}

public static void func()


{
a = a + 1;
System.out.println ("a = " + a);
}

public static void main(String[] args)


{

Test obj = new Test();


obj.func();

}
}

inside static block


a = 4
inside constructor
a = 11

Compiler Error

Run Time Error

inside static block


a = 4
inside constructor
a = 5

inside static block


a = 10
inside constructor
a = 11

Discuss it

Question 2 ‒ Explanation
Static blocks are called before constructors. Therefore, on object creation of class Test, static block is called. So, s
tatic variable a = 4. Then constructor Test() is called which assigns a = 10. Finally, function func() increments its v
alue.

Question 3

Java

final class Complex {


private double re, im;
public Complex(double re, double im) {
this.re = re;
this.im = im;
}
Complex(Complex c)
{
System.out.println("Copy constructor called");
re = c.re;
im = c.im;
}
public String toString() {
return "(" + re + " + " + im + "i)";
}
}
class Main {
public static void main(String[] args) {
Complex c1 = new Complex(10, 15);
Complex c2 = new Complex(c1);
Complex c3 = c1;
System.out.println(c2);
}
}

Copy constructor called


(10.0 + 15.0i)

Copy constructor called


(0.0 + 0.0i)

(10.0 + 15.0i)

(0.0 + 0.0i)

Discuss it

Question 4

Output of following Java program

Java

class Point {
int m_x, m_y;

public Point(int x, int y) { m_x = x; m_y = y; }


public Point() { this(10, 10); }
public int getX() { return m_x; }
public int getY() { return m_y; }

public static void main(String args[]) {


Point p = new Point();
System.out.println(p.getX());
}
}

10

compiler error

Discuss it
Question 4 ‒ Explanation
it\'s a simple program where constructor is called with parameters and values are initialized.

Question 5

Which of the following is/are true about constructors in Java?

1) Constructor name should be same as class name.


2) If you don\'t define a constructor for a class,
a default parameterless constructor is automatically
created by the compiler.
3) The default constructor calls super() and initializes all
instance variables to default value like 0, null.
4) If we want to parent class constructor, it must be called in
first line of constructor.

1, 2

1, 2 and 3

1, 2, 3 and 4

Discuss it

Question 6

Predict the output?

Java

package main;
class T {
int t = 20;
}
class Main {
public static void main(String args[]) {
T t1 = new T();
System.out.println(t1.t);
}
}

20

Compiler Error
Discuss it

Question 6 ‒ Explanation
In Java, member variables can assigned a value with declaration. In C++, only static const variables can be assign
ed like this.

Question 7

Predict the output of following Java program

Java

class T {
int t = 20;
T() {
t = 40;
}
}
class Main {
public static void main(String args[]) {
T t1 = new T();
System.out.println(t1.t);
}
}

20

40

Compiler Error

Discuss it

Question 7 ‒ Explanation
The values assigned inside constructor overwrite the values initialized with declaration.

You have completed 7/7 questions .


Your accuracy is 85%.

Last Updated : Mar 22, 2024


Take a part in the ongoing discussion View All Discussion

Company Languages DSA Data Web Python


About Us Python Data Science & Technologies Tutorial
Corporate & Legal Java Structures
Communications Address:- In Media
ML HTML Python
C++ Algorithms CSS Programming
A-143, 9th Floor, Sovereign Data Science
Corporate Tower, Sector- Contact Us PHP DSA for JavaScript Examples
With Python
136, Noida, Uttar Pradesh Advertise with GoLang Beginners TypeScript Python Projects
(201305) | Registered Data Science
us SQL Basic DSA ReactJS Python Tkinter
Address:- K 061, Tower K, For Beginner
GFG Corporate R Language Problems NextJS Web Scraping
Gulshan Vivante Apartment, Solution
Machine
Sector 137, Noida, Gautam Android DSA Bootstrap OpenCV Tutorial
Learning
Buddh Nagar, Uttar Pradesh, Placement Tutorial Roadmap Web Design Python Interview
ML Maths
201305 Training Tutorials Top 100 Question
Data
Program Archive DSA Django
Visualisation
GeeksforGeeks Interview
Pandas
Community Problems
NumPy
DSA
NLP
Roadmap
Deep Learning
by
Sandeep
Jain
All Cheat
Sheets

Computer DevOps System Inteview School GeeksforGeeks


Science Git Design Preparation Subjects Videos
Operating Linux High Level Competitive Mathematics DSA
Systems AWS Design Programming Physics Python
Computer Docker Low Level Top DS or Algo Chemistry Java
Network Kubernetes Design for CP Biology C++
Database Azure UML Company-Wise Social Science Web Development
Management GCP Diagrams Recruitment English Data Science
System DevOps Interview Process Grammar CS Subjects
Software Roadmap Guide Company-Wise Commerce
Engineering Design Preparation World GK
Digital Logic Patterns Aptitude
Design OOAD Preparation
Engineering System Puzzles
Maths Design
Software Bootcamp
Development Interview
Software Questions
Testing

@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved

You might also like