Question 1
Is there any compiler error in the below Java program?
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();
}
}
Question 2
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();
}
}
Question 3
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);
}
}
Question 4
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());
}
}
Question 5
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.
Question 6
package main;
class T {
int t = 20;
}
class Main {
public static void main(String args[]) {
T t1 = new T();
System.out.println(t1.t);
}
}
Question 7
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);
}
}
There are 7 questions to complete.