Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Q 1-4. Does the following code result in an compiler error? 1) HashMap> msgs = n

ID: 3539639 • Letter: Q

Question

Q 1-4. Does the following code result in an compiler error?                 



1)            HashMap> msgs = new HashMap>();


a)      YES                              b) NO



2)            Integer num = 2;


num += new Integer(num) * new Integer(2);


a) YES                   b) NO                    



                               


Questions 3-6 refer to the following 2D array of String elements.             


String[ ][ ] words = new String[ ][ ] {


                                                                                { "The" , "time" , null , "has" } ,


                                                                                null ,


                                                                                { "come", "the" , "walrus" , "said" , } ,


                                                                                { "to" , "talk" , "of" } ,


                                                                                { "many" , "things" } ,


                                                                        };



3) Look carefully at the above code, is the above code an error?                            


a)      YES              b) NO      (if you answered yes, fix the problem(s) with your pen/pencil)


4) What is the value words[1].length ?


a)      Compiler-error            b) Runtime-error c)



5) What is the value of words[2][1].length ?


a)      Compiler-error            b) Runtime-error c)



6) Write code below to swap the two bold lines.   



7) Write a static method called getString that returns a String and takes a 2D array of Strings as a parameter. The returned String will contain all the lines of words with added where appropriate - your code should skip all null entries in the 2D array and have no-compiler or run-time errors.


e.g. if you called getString with the words 2D array it would return =>


"The time has


come the walrus said


to talk of


many things"       


               


The Person class has an abstract method called getBonus.


The Student, Staff and Freshman class all have methods called getBonus that are not abstract.


(True | False)         1 mark each


8)      Senior class has an instance variable id that it can access directly.                                 T | F


9)      The Senior class must have an import statement.                                                                               T | F


10) The Senior class must be abstract                                                                                                            T | F


11) Senior class can make an object of type Student and access probation.                         T | F


12) The Person class can make an object of type Staff and access its salary                       T | F


13) If I make an object of type Senior, a constructor in Staff must be called                       T | F


14) The Student class can make an object of type Student and access its probation          T | F

The Student class

15) A constructor that takes a name, id, age and probation. Set the instance variables. Last two lines are done for you            

               

                16) Make a no-argument constructorwithoutcalling super. Last 2 lines done for you. Use %u201Cnone%u201D, %u201Cnone%u201D, 0, false as default values.

                17) Make a copy constructor

                18) Make an equals method(overrides Object equal) Two students are equal only if they have the same id and their ages are the same.










Explanation / Answer

Q 1-4. Does the following code result in an compiler error?           

1) HashMap<> msgs = new HashMap<>();

a) YES
correct declaration as follows:
      HashMap msgs = new HashMap();

2) Integer num = 2;
num += new Integer(num) * new Integer(2);
b) NO                   

String[ ][ ] words = new String[ ][ ] {{ "The" , "time" , null , "has" } ,
null ,
{ "come", "the" , "walrus" , "said" , } ,
{ "to" , "talk" , "of" } ,
{ "many" , "things" } ,
};

b) NO      (if you answered yes, fix the problem(s) with your pen/pencil)

4) What is the value words[1].length ?

b) Runtime-error (words[1] points to null, results in run-time error

5) What is the value of words[2][1].length ?

a) Compiler-error          

correct declaration is words[2][1].length();

6) Write code below to swap the two bold lines.  

public static void swap(String[] line)
{
String temp = line[0];
line[0] = line[1];
line[1] = temp;
}

7) Write a static method called getString that returns a String and takes a 2D array of Strings as a parameter.
The returned String will contain all the lines of words with added where appropriate -
your code should skip all null entries in the 2D array and have no-compiler or run-time errors.

public static String getString(String[][] words) throws Exception
{
    StringBuilder sb= new StringBuilder();

for(int i=0; i<words.length; i++)
{
try{
for(int j=0; (j<words[i].length); j++)
{
if(words[i][j]!=null)
sb.append(words[i][j] + " ");
}
sb.append(" ");
}
catch(Exception e)
{
    continue;
}
}


return sb.toString();
}

}               


The Person class has an abstract method called getBonus.


The Student, Staff and Freshman class all have methods called getBonus that are not abstract.

(True

8)      Senior class has an instance variable id that it can access directly.       T

9)      The Senior class must have an import statement.       T

10) The Senior class must be abstract    F

11) Senior class can make an object of type Student and access probation. T

12) The Person class can make an object of type Staff and access its salary    T

13) If I make an object of type Senior, a constructor in Staff must be called F

14) The Student class can make an object of type Student and access its probation          T

The Student class

15) A constructor that takes a name, id, age and probation. Set the instance variables. Last two lines are done for you  

public Student(String name,int id, int age, boolean probation)
{
super(name,id,age);
this.probation=probation;
}       

16) Make a no-argument constructorwithoutcalling super. Last 2 lines done for you. Use %u201Cnone%u201D, %u201Cnone%u201D, 0, false as default values.

public Person()
{
name = "none";
id =0;
age = 0;
}

public Student()
{
this.probation=false;
}    

17) Make a copy constructor

public Student(Student new)
{
this.name = new.getName();
this.id = new.getId();
this.age = new.getAge();
this.probation=new.getProbation();
}

18) Make an equals method(overrides Object equal) Two students are equal only if they have the same id and their ages are the same.

public boolean equals(Student s)
{
return (s.getAge() == this.getAge() && s.getId() == this.getId());
}