Course Hero Logo

Q3 given the code fragment 1 public class employee 2

  • No School
  • AA 1
  • 137
  • 100% (3) 3 out of 3 people found this document helpful

Course Hero uses AI to attempt to automatically extract content from documents to surface to you and others so you can study better, e.g., in search results, to enrich docs, and more. This preview shows page 94 - 99 out of 137 pages.

Q3. Given the code Fragment:1)public classEmployee2){3)String name;4)booleancontract;5)doublesalary;6)Employee()7){8)//line-19)}10)publicString toString()11){12)returnname+":"+contract+":"+salary;13)}14)public static voidmain(String[] args)15){16)Employee e =newEmployee();
9517)//Line-218)System.out.println(e);19)}20)}Which 2 modifications, when made independently,enable to the code to printDurga:true:100.0A. Replace line-2 withe.name="Durga";e.contract=true;e.salary=100;B. Replace line-2 withthis.name="Durga";this.contract=true;this.salary=100;C. Replace line-1 withthis.name=new String("Durga");this.contract= new Boolean(true);this.salary= new Double(100);D. Replace line-1 withname="Durga";contract=TRUE;salary=100.0f;E. Replace line-1 with:this("Durga",true,100)Answer: A and CExplanation:Inside main method(static method) we cannot use this and super keywords.We can assign wrapper object to the primitive and compiler will perform requiredconversions, which is also known as AutoUnboxing.
96Q4. Given:1)classA2){3)publicA()4){5)System.out.println("A");6)}7)}8)classBextendsA9){10)publicB()11){12)//line-113)System.out.println("B");14)}15)}16)classCextendsB17){18)publicC()19){20)//line-221)System.out.println("C");22)}23)public static voidmain(String[] args)24){25)C c =newC();26)}27)}What is the Result?A. CBAB. CC. ABCD. Compilation Fails at line-1 and line-2Answer: CExplanation: Whenever we are creating child class object then both parent class and childclass constructors will be executed,but first parent class constructor followed by childclass constructor.
97Q5. Given1)classVehicle2){3)intx;4)Vehicle()5){6)this(10);// line-17)}8)Vehicle(intx)9){10)this.x=x;11)}12)}13)classCarextendsVehicle14){15)inty;16)Car()17){18)super();19)this(20);//line-220)}21)Car(inty)22){23)this.y= y;24)}25)publicString toString()26){27)return super.x+":"+this.y;28)}29)}And given the code fragment:Vehicle v= new Car();System.out.println(v);What is the result?A. 10:20B. 0:20C. Compilation Fails at Line-1D. Compilation Fails at Line-2
98Answer: DExplanation:super() and this() should be only in first line of constructor,otherwise we will get error.Q6. Given the code fragment:1)public classPerson2){3)String name;4)intage=25;5)publicPerson(String name)6){7)this();//line-18)setName(name);9)}10)publicPerson(String name,intage)11){12)Person(name);//Line-213)setAge(age);14)}15)//setter and getter methods go here16)publicString show()17){

Upload your study docs or become a

Course Hero member to access this document

Upload your study docs or become a

Course Hero member to access this document

End of preview. Want to read all 137 pages?

Upload your study docs or become a

Course Hero member to access this document

Term
Fall
Professor
NoProfessor
Tags

Newly uploaded documents

Show More

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture