1) Which of the following class definitions is correct in Java? (i) public class
ID: 3639927 • Letter: 1
Question
1)
Which of the following class definitions is correct in Java?
(i)
public class Employee
{
private String name;
private double salary;
private int id;
public Employee()
{
name = "";
salary = 0.0;
id = 0;
}
public Employee(String n, double s, int i)
{
name = n;
salary = s;
id = i;
}
public void print()
{
System.out.println(name + " " + id + " " + salary);
}
}
(ii)
public class Employee
{
private String name;
private double salary;
private int id;
public void Employee()
{
name = "";
salary = 0.0;
id = 0;
}
public void Employee(String n, double s, int i)
{
name = n;
salary = s;
id = i;
}
public void print()
{
System.out.println(name + " " + id + " " + salary);
}
}
a) Only (i)
b) Only (ii)
c) Both (i) and (ii)
d) Neither is correct
2)
public class Secret
{
private int x;
private static int y;
public static int count;
public int z;
public Secret()
{
x = 0;
z = 1;
}
public Secret(int a)
{
x = a;
}
public Secret(int a, int b)
{
x = a;
y = b;
}
public String toString()
{
return ("x = " + x + ", y = " + y + ",
count = " + count);
}
public static void incrementY()
{
y++;
}
}
What does the default constructor do in the class definition in the accompanying figure?
a) Sets the value of x to 0
b) Sets the value of z to 1
c) Sets the value of x to 0 and the value of z to 1
d) There is no default constructor.
3)
public class Secret
{
private int x;
private static int y;
public static int count;
public int z;
public Secret()
{
x = 0;
z = 1;
}
public Secret(int a)
{
x = a;
}
public Secret(int a, int b)
{
x = a;
y = b;
}
public String toString()
{
return ("x = " + x + ", y = " + y + ",
count = " + count);
}
public static void incrementY()
{
y++;
}
}
What might the heading of the copy constructor for the class in the accompanying figure look like?
a) public Secret(Secret s)
b) private copySecret()
c) Secret copySecret = new Secret()
d) public copySecret(int a, int b)
4)
public class Secret
{
private int x;
private static int y;
public static int count;
public int z;
public Secret()
{
x = 0;
z = 1;
}
public Secret(int a)
{
x = a;
}
public Secret(int a, int b)
{
x = a;
y = b;
}
public String toString()
{
return ("x = " + x + ", y = " + y + ",
count = " + count);
}
public static void incrementY()
{
y++;
}
}
Based on the class in the accompanying figure, which of the following statements is illegal?
a) Secret.incrementY();
b) Secret.count++;
c) Secret.z++;
d) Secret secret = new Secret(4);
Explanation / Answer
please rate - thanks
1)
Which of the following class definitions is correct in Java?
(i)
public class Employee
{
private String name;
private double salary;
private int id;
public Employee()
{
name = "";
salary = 0.0;
id = 0;
}
public Employee(String n, double s, int i)
{
name = n;
salary = s;
id = i;
}
public void print()
{
System.out.println(name + " " + id + " " + salary);
}
}
(ii)
public class Employee
{
private String name;
private double salary;
private int id;
public void Employee()
{
name = "";
salary = 0.0;
id = 0;
}
public void Employee(String n, double s, int i)
{
name = n;
salary = s;
id = i;
}
public void print()
{
System.out.println(name + " " + id + " " + salary);
}
}
a) Only (i)
b) Only (ii)
c) Both (i) and (ii)
d) Neither is correct
2)
public class Secret
{
private int x;
private static int y;
public static int count;
public int z;
public Secret()
{
x = 0;
z = 1;
}
public Secret(int a)
{
x = a;
}
public Secret(int a, int b)
{
x = a;
y = b;
}
public String toString()
{
return ("x = " + x + ", y = " + y + ",
count = " + count);
}
public static void incrementY()
{
y++;
}
}
What does the default constructor do in the class definition in the accompanying figure?
a) Sets the value of x to 0
b) Sets the value of z to 1
c) Sets the value of x to 0 and the value of z to 1
d) There is no default constructor.
3)
public class Secret
{
private int x;
private static int y;
public static int count;
public int z;
public Secret()
{
x = 0;
z = 1;
}
public Secret(int a)
{
x = a;
}
public Secret(int a, int b)
{
x = a;
y = b;
}
public String toString()
{
return ("x = " + x + ", y = " + y + ",
count = " + count);
}
public static void incrementY()
{
y++;
}
}
What might the heading of the copy constructor for the class in the accompanying figure look like?
a) public Secret(Secret s)
b) private copySecret()
c) Secret copySecret = new Secret()
d) public copySecret(int a, int b)
4)
public class Secret
{
private int x;
private static int y;
public static int count;
public int z;
public Secret()
{
x = 0;
z = 1;
}
public Secret(int a)
{
x = a;
}
public Secret(int a, int b)
{
x = a;
y = b;
}
public String toString()
{
return ("x = " + x + ", y = " + y + ",
count = " + count);
}
public static void incrementY()
{
y++;
}
}
Based on the class in the accompanying figure, which of the following statements is illegal?
a) Secret.incrementY(); all of these you would reference using the object name, not the class name
b) Secret.count++;
c) Secret.z++;
d) Secret secret = new Secret(4);
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.