Explain why the underlined code is wrong. A. (Syntaxerrors) public class Test{ p
ID: 3610394 • Letter: E
Question
Explain why the underlined code is wrong.
A. (Syntaxerrors)
public class Test{
privateint x;
publicstatic void main(String[] args) {
new Test();
}
publicTest(int x) {
this.x = x;
}
}
B. (Syntax errors)
public class Test{
publicstatic void main(String[] args) {
A a = new A(5.5);
System.out.println(a.x);
}
}
public class A{
privatex;
publicvoid A(double x) {
this.x = x;
}
}
C. (Syntax errors)
public class A{
String[] myStrings =new String[2];
myStrings[0] = newString("A");
public A( ){
}
}
D. (Runtimeerror)
public class Test{
publicstatic void main(String[] args) {
Object object = new Fruit();
Object object1 = (Apple)object;
}
}
class Appleextends Fruit {
}
class Fruit {
}
Explanation / Answer
Dear User, a) newTest(); // This statement is wrong,becausethis is not a syntax to create the object of the class TEST. We cancreate the object like Test obj1 = new Test( ); b) System.out.println(a.x); // This is wrongbecause the does not have method x or variable x privatex; // This is wrong, because private is not a data type.It is access specifier.So, don't declare like that we candeclare private: int x; //like this we can declared publicvoid A(double x) // This statement is wrong,because constructordoes not have any return type.Here A is the constructor.we can writelike this A( double x) { } c) myStrings[0] = new String("A"); // This is wrong,because dynamically memory is created,so don't create memory once again. Thiscan be write like this myStrings[0] = "A"; d) Objectobject1 = (Apple)object; //This statement iswrong,because we didn't have object class. If you wantto create object for Apple, we can write like Apple obj1 = new Apple(); I hope this will helps toyouRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.