Java.pdf
Document Details
Uploaded by PolishedMagic
2020
Tags
Full Transcript
LUBNA KHAN. Dashboard / Java / Classes and Objects, Packages / Test your understanding - Packages Started on Monday, 3 February 2020, 1:34 AM...
LUBNA KHAN. Dashboard / Java / Classes and Objects, Packages / Test your understanding - Packages Started on Monday, 3 February 2020, 1:34 AM State Finished Completed on Monday, 3 February 2020, 1:35 AM Time taken 59 secs Marks 4.00/5.00 Grade 80.00 out of 100.00 Feedback Congratulations!!! You have passed by securing more than 80% Question Given the below code : 1 import static java.lang.Math.PI; Correct Mark 1.00 out public class AreaCalculator { of 1.00 public double calculateArea(double radius) { double area = PI * radius * radius; //Instead of Math.PI return area; } } Drag and drop the correct statement to use the static variable PI. import java.lang.Math.PI; import static java.lang.Math; Your answer is correct. The correct answer is: Given the below code : [import static java.lang.Math.PI; ] public class AreaCalculator { public double calculateArea(double radius) { double area = PI * radius * radius; //Instead of Math.PI return area; } } Drag and drop the correct statement to use the static variable PI. Question LUBNA KHAN. Assume class Calculator in package p1 and CalculatorService class in package p2 as shownbelow. 2 package p1; Incorrect Mark 0.00 out public class Calculator { of 1.00 __________ static void calculate(){ //some code here } } package p2; import p1.Calculator; public class CalculatorService { public void display(){ Calculator.calculate(); } } What can be the valid access specifier for the calculate method in Calculator class so that it can be accessed from CalculatorService class? Select one: a. default b. protected c. private d. Any access specifier except private e. public If a method defined within a class in one package has to be invoked from outside, then that method has to be declared public. The correct answer is: public Question Given the class Book and Library in two different packages : 3 1. package model; Correct 2. public class Book { Mark 1.00 out 3. private static void countBook( ) { } of 1.00 4. } 1. package util; 2. public class Library { 3. public static void main(String[] args) { 4. // insert code here 5. } 6. } What is required at line 4 in class Library to use the countBook method of Book class? Select one: a. model.Book.countBook(); b. util.Library.countBook(); c. Book.countBook(); d. countBook(); e. Library class cannot use the countBook method in Book class. countBook() cannot be invoked since the method is declared private. The correct answer is: Library class cannot use the countBook method in Book class. Question Rearrange this code correctly. LUBNA KHAN. 4 Correct package test; Mark 1.00 out of 1.00 import java.util.Scanner; public class Main { //Some code here } Your answer is correct. Question Given the class Book in packages p1 and class Main in package p2. In main to create an object of Book, which of the 5 following are valid. Correct Mark 1.00 out of 1.00 Select one: a. p1.Book bookObj=new Book(); b. p1.Book bookObj=new p1.Book(); c. Book bookObj=new Book(); Since the Book class is present within p1 package, it has to be accessed as "p1.Book", everytime, from package p2. The correct answer is: p1.Book bookObj=new p1.Book(); Reshma Banu Mothadi Shaik. Dashboard / Java / Arrays and Strings / Pre-Quiz Started on Friday, 27 March 2020, 12:52 AM State Finished Completed on Friday, 27 March 2020, 12:55 AM Time taken 2 mins 54 secs Marks 7.00/8.00 Grade 87.50 out of 100.00 Feedback Congratulations!! You have passed by securing more than 80% Question A JavaBeans component has the following field: 1 private boolean enabled; Correct Which two pairs of method declarations follow the JavaBeans standard for accessing this field? (Choose two.) Mark 1.00 out of 1.00 Select one or more: a. public void setEnabled( boolean enabled ) public boolean getEnabled() b. public void setEnabled( boolean enabled ) public void isEnabled() c. public void setEnabled( boolean enabled ) public boolean isEnabled() d. public boolean setEnabled( boolean enabled ) public boolean getEnabled() Your answer is correct. When writing getters and setters, setters return type is void and getters return type is the corresponding data type. Naming convention is camelcase notation. For setters start with set followed by field name and for getters start with get followed by field name. For boolean return type, it should start with 'is' or 'are' followed by field name. The correct answers are: public void setEnabled( boolean enabled ) public boolean isEnabled(), public void setEnabled( boolean enabled ) public boolean getEnabled() Question Predict the Output of following Java Program. 2 class Test { Correct int x = 10; Mark 1.00 out public static void main(String[] args) { of 1.00 System.out.println(x); } } Select one: a. 0 b. 10 c. Runtime Exception d. Compile Time Error Your answer is correct. The correct answer is: Compile Time Error Question Reshma Banu Mothadi Shaik. Identify the true statement(s). 3 Statement 1 : When no constructor is written in a class, the compiler creates a default constructor Correct Statement 2 : The default constructor will implicitly invoke the default / no-argument constructor of the super class Mark 1.00 out Statement 3 : If a class has a parametrized constructor alone, then the compiler will create the default constructor of 1.00 Statement 4 : If a class has a parametrized constructor, it is mandatory to write a no-argument constructor Select one: a. 1, 2 and 4 b. 1, 2 and 3 c. 2 and 3 d. 1 and 2 Your answer is correct. The correct answer is: 1 and 2 Question Identify which statement is true about construtors. 4 Correct Mark 1.00 out Select one: of 1.00 a. Constructor of a class should not have a return type, which means the return type is void b. Constructor can be overloaded c. Constructor will be invoked explicitly like other methods d. Constructor should have same name as class name, but not case sensitive Your answer is correct. The correct answer is: Constructor can be overloaded Question Given: Reshma Banu Mothadi Shaik. 5 public class ItemTest { Correct private final int id; Mark 1.00 out public ItemTest(int id) { of 1.00 this.id = id; } public void updateId(int newId) { id = newId; } public static void main(String[] args) { ItemTest fa = new ItemTest(42); fa.updateId(69); System.out.println(fa.id); } } What is the result? Select one: a. CompileTime Error b. Runtime Error c. 69 Your answer is correct. The correct answer is: CompileTime Error Question Reshma Banu Mothadi Shaik. package edu.ABC.model; 6 public class Account { Incorrect public static final float INTERTEST_RATE = 7.5; Mark 0.00 out of 1.00 } Identify the correct options from the classes provided below. Select one or more: a. package edu.ABC.model; public class Loan { public double getInterest() { return INTEREST_RATE; } } b. import edu.ABC.model.Account ; public class Loan { public double getInterest() { return Account.INTEREST_RATE; } } c. import static edu.ABC.model.Account ; public class Loan { public double getInterest() { return INTEREST_RATE; } } d. import static edu.ABC.model.Account.*; public class Loan { public double getInterest() { return INTEREST_RATE; } } Your answer is incorrect. The correct answers are: import static edu.ABC.model.Account.*; public class Loan { public double getInterest() { return INTEREST_RATE; } }, import edu.ABC.model.Account ; public class Loan { public double getInterest() { return Account.INTEREST_RATE; } } Question Which members of a class can be accessed by other classes is determined by the ________________ 7 Correct Select one: Mark 1.00 out a. Access specifier of 1.00 b. class c. constructor d. variables Your answer is correct. The correct answer is: Access specifier Question What does this() mean in constructor chaining concept? Reshma Banu Mothadi Shaik. 8 Correct Select one: Mark 1.00 out a. Used for calling the no argument constructor of the same class. of 1.00 b. Used for calling the current object of the same class. c. Used for calling the current object of the parent class. d. Used for calling the parameterized constructor of the parent class. Your answer is correct. The correct answer is: Used for calling the no argument constructor of the same class. Reshma Banu Mothadi Shaik. Dashboard / Java / Inheritance, Polymorphism, Abstract class, Interface / Post-Quiz Started on Friday, 27 March 2020, 1:27 AM State Finished Completed on Friday, 27 March 2020, 1:33 AM Time taken 6 mins 11 secs Marks 16.00/16.00 Grade 100.00 out of 100.00 Feedback Congratulations!! You have passed by securing more than 80% Question Assume Book is a parent class and Magazine class is the child of Book class. 1 Match the following: Correct Mark 1.00 out of 1.00 Book b = new Magazine(); Upcasting Magazine m = (Magazine) b Downcasting Your answer is correct. The correct answer is: Book b = new Magazine(); → Upcasting, Magazine m = (Magazine) b → Downcasting Question Reshma Banu Mothadi Shaik. Given: 2 1. public class Employee { Correct 2. String name; Mark 1.00 out 3. double baseSalary; of 1.00 4. Employee(String name, double baseSalary) { 5. this.name = name; 6. this.baseSalary = baseSalary; 7. } 8. } And: 11. public class Salesperson extends Employee { 12. double commission; 13. public Salesperson(String name, double baseSalary, 14. double commission) { 15. // insert code here 16. } 17. } Which code, inserted at line 17, completes the Salesperson constructor? Select one: a. super(name, baseSalary); this.commission = commission; b. super(); this.commission =commission; c. super(); commission = commission; d. this.commission = commission; super(); e. this.commission = commission; super(name, baseSalary); f. this.commission = commission; Your answer is correct. The correct answer is: super(name, baseSalary); this.commission = commission; Question A default method in an interface can be either private or public or protected. State True or False. 3 Correct Mark 1.00 out Select one: of 1.00 True False The correct answer is 'False'. Question Reshma Banu Mothadi Shaik. Which would declare a compilable abstract class? 4 Correct Select one: Mark 1.00 out a. of 1.00 public abstract class Shape { public Square draw(); } b. public class Shape { public abstract Square draw(); } c. public abstract class Shape { public Square draw() { } } d. public class Shape abstract { public abstract Square draw(); } Your answer is correct. The correct answer is: public abstract class Shape { public Square draw() { } } Question public abstract class Shape { 5 private int x; private int y; Correct public abstract void draw(); Mark 1.00 out public void setAnchor(int x, int y) { of 1.00 this.x = x; this.y = y; } } Which two classes use the Shape class correctly? (Choose two.) Select one or more: a. public class Circle extends Shape { private int radius; public void draw(); } b. public abstract class Circle extends Shape { private int radius; } c. public class Circle extends Shape { private int radius; public void draw() {} } d. public class Circle implements Shape { private int radius; } Your answer is correct. If a class inherits an abstract class with abstract methods, it should provide implementation for all the abstract methods in the parent. If not, then that class needs to be declared as abstract. The correct answers are: public abstract class Circle extends Shape { private int radius; }, public class Circle extends Shape { private int radius; public void draw() {} } Question Reshma Banu Mothadi Shaik. A class can be declared as __________ if it should not be sub classed. 6 Correct Mark 1.00 out Select one: of 1.00 a. final b. protected c. public d. private Your answer is correct. The correct answer is: final Question If a method in a super class is overridden by the sub class, then the overridden method can be invoked using ____________ 7 keyword. Correct Mark 1.00 out of 1.00 Select one: a. class b. super c. this d. extends Your answer is correct. The correct answer is: super Question If a class inheriting an abstract class does not provide definition for all abstract methods in the parent class, then it will be 8 known as _______________. Correct Select one: Mark 1.00 out a. Static class of 1.00 b. A concrete class c. abstract d. A simple class Your answer is correct. The correct answer is: abstract Question ____________ can be achieved through inheritance. 9 Correct Select one: Mark 1.00 out a. code reusability of 1.00 b. run time polymorphism c. both run time polymorphism & code reusability d. none of the options Your answer is correct. The correct answer is: both run time polymorphism & code reusability Question Reshma Banu Mothadi Shaik. If a method in an interface is implemented, then that method should be either _______ or _________. 10 Correct Mark 1.00 out Select one: of 1.00 a. static, default b. public , abstra c. abstract, default d. abstract, static Your answer is correct. The correct answer is: static, default Question Which of the following represents the correct lambda expression for the Functional method : 11 int findMax(int a,int b) ? Correct Mark 1.00 out of 1.00 Select one: a. (a, b) -> { int min = a>b ? a : b; } b. (int a,int b) -> int min = a>b ? a : b; return min; c. (int a,int b) -> { int min = a>b ? a : b; return min; } d. (int a,int b) -> { min = a>b ? a : b; return min; } Your answer is correct. The correct answer is: (int a,int b) -> { int min = a>b ? a : b; return min; } Question Reshma Banu Mothadi Shaik. 12 B A Correct Mark 4.00 out class A { of 4.00 String name="A"; public String getName() { return name; } String greeting() { return "class A"; } } class B extends A { String name="B"; String greeting() { return "class B"; } } public class Test { public static void main(String arg[]) { A a=new A(); A b=new B(); System.out.println(a.greeting()+" has name "+a.getName()); System.out.println(b.greeting()+" has name "+b.getName()); } } Place the names "A" and "B" in the following output. class A has name A class B has name A Question Predict the output: Reshma Banu Mothadi Shaik. 13 public abstract class Abs { Correct public Abs(){ Mark 1.00 out System.out.println("Constructor from Abstract class"); of 1.00 } } public class Test extends Abs { public static void main(String args[]){ Abs obj=new Test(); } } Select one: a. Compile time error: An abstract class cannot have a constructor b. Compile time error: An abstract class cannot be instantiated c. Constructor from Abstract class d. Program will execute successfully but not display anything Your answer is correct. In the constructor of the child class, the first line should be a call to the super class constructor. If not written, then implicitly it invokes the super class constructor as super(); The correct answer is: Constructor from Abstract class 31/03/2020 Pre-Quiz: Attempt review Adeeb Khan. Dashboard / Java / Inheritance, Polymorphism, Abstract class, Interface / Pre-Quiz Started on Tuesday, 31 March 2020, 12:58 AM State Finished Completed on Tuesday, 31 March 2020, 1:05 AM Time taken 6 mins 53 secs Marks 11.00/13.00 Grade 84.62 out of 100.00 Feedback Congratulations!! You have passed by securing more than 80% Question Given: 1 21. public String makingStrings() { Correct 22. String str = "Welcome"; Mark 1.00 out 23. str = str.substring(1, 5); of 1.00 24. str = str.toUpperCase(); 25. return str; 26. } How many String objects will be created when this method is invoked? Select one: a. 4 b. 3 c. 2 d. 1 Your answer is correct. The correct answer is: 3 Question To copy elements from one array to another array, Java provides efficient built-in methods in ________ class. 2 Correct Mark 1.00 out Select one: of 1.00 a. Object b. Array c. Arrays d. Collection Your answer is correct. The correct answer is: Arrays https://accenturelearning.tekstac.com/mod/quiz/review.php?attempt=1036832&cmid=1020#q4 1/6 31/03/2020 Pre-Quiz: Attempt review Question Given: Adeeb Khan. 3 public class Main { Correct public static void test(String str) { Mark 1.00 out if (str == null | str.length() == 0) { of 1.00 System.out.println("String is empty"); } else { System.out.println("String is not empty"); } } public static void main(String a[]) { test(null); } } What is the result? Select one: a. An exception is thrown at runtime. b. "String is empty" is printed to output. c. Compilation fails d. "String is not empty" is printed to output. Your answer is correct. The correct answer is: An exception is thrown at runtime. Question Determine the output: 4 public class Test { Correct public static void main(String[] args) { Mark 1.00 out int[] arr1 = {11,12,13,14,14,15}; of 1.00 int[] arr2 = arr1; arr1 = new int; for(int i = 0; i < arr1.length; i++) System.out.print(arr2[i] + " "); } } Select one: a. 11 12 13 14 15 b. 0 0 0 0 0 c. 0 0 0 d. 11 12 13 Your answer is correct. The correct answer is: 11 12 13 https://accenturelearning.tekstac.com/mod/quiz/review.php?attempt=1036832&cmid=1020#q4 2/6 31/03/2020 Pre-Quiz: Attempt review Question Adeeb Khan. 5 Choose correct answer – true / false Correct String[] cities = new String[]{"London", "Paris", "NewYork", "HongKong", "Tokyo"}; Mark 3.00 out String[] metros = new String[]{"London", "Paris", "NewYork", "HongKong", "Tokyo"}; of 3.00 String[] capitals = cities; System.out.println("cities == metros : " + (cities == metros)); System.out.println("cities == capitals : " + (cities == capitals)); System.out.println("cities.equals(metros) : " + cities.equals(metros)); System.out.println("cities.equals(capitals) : " + cities.equals(capitals)); System.out.println("Arrays.equals(cities, metros) : " + Arrays.equals(cities, metros)); System.out.println("Arrays.equals(cities, capitals) : " + Arrays.equals(cities, capitals)); What will be the correct answer for the following? cities.equals(capitals) : true Arrays.equals(cities, metros) : true cities == metros : false Question In Java, what happens if you try to compile the below code : 6 class MyStringDemo extends String { Incorrect } Mark 0.00 out of 1.00 Select one: a. The code does not compile because the String class is final. b. The code compiles successfully. c. The code does not compile because the String class is abstract. d. The code does not compile because you have not defined a main() method. Your answer is incorrect. The correct answer is: The code does not compile because the String class is final. https://accenturelearning.tekstac.com/mod/quiz/review.php?attempt=1036832&cmid=1020#q4 3/6 31/03/2020 Pre-Quiz: Attempt review Question Adeeb Khan. Observe the code 7 public class StringDemo { Correct public static void main(String[]args) { Mark 1.00 out of 1.00 String s1=new String("java"); String s2=new String("java"); System.out.println(s2.compareTo(s1)); } } The output of the code is Select one: a. 1 b. 0 c. false d. true Your answer is correct. The correct answer is: 0 Question What will be the output of the program? 8 public class Test { Correct public static void main (String args[]) { Mark 1.00 out of 1.00 String str = NULL; System.out.println(str); } } Select one: a. Compile time error b. Runtime Exception c. NULL d. Code runs but no output Your answer is correct. The correct answer is: Compile time error https://accenturelearning.tekstac.com/mod/quiz/review.php?attempt=1036832&cmid=1020#q4 4/6 31/03/2020 Pre-Quiz: Attempt review Question What will be the output of the program? Adeeb Khan. 9 String str = "welcome"; Correct str.toUpperCase(); Mark 1.00 out of 1.00 String s1 = str.replace('C', 'c'); s1 = s1 + " to Java"; System.out.println(s1); Select one: a. WELcOME to Java b. welcome to Java c. WELCOME TO JAVA d. WELCOME to Java Your answer is correct. The correct answer is: welcome to Java Question Predict the output: 10 public class Trial { Correct public static void main(String[] args) { Mark 1.00 out int arr={ }; of 1.00 System.out.print(arr); } } Select one: a. Compile time error b. Runtime error c. ArrayIndexOutOfBoundsException d. 0 Your answer is correct. The correct answer is: Compile time error https://accenturelearning.tekstac.com/mod/quiz/review.php?attempt=1036832&cmid=1020#q4 5/6 31/03/2020 Pre-Quiz: Attempt review Adeeb Khan. Question Observe the code. 11 Incorrect int arr[]={12,4,22,5,1,66}; Mark 0.00 out int position = Arrays.binarySearch(arr,5); of 1.00 System.out.println(position); What will be the output of the above code snippet? Select one: a. Unpredictable b. Compilation error c. 3 d. 2 Your answer is incorrect. The correct answer is: Unpredictable https://accenturelearning.tekstac.com/mod/quiz/review.php?attempt=1036832&cmid=1020#q4 6/6 Adeeb Khan. Dashboard / Java / Inheritance, Polymorphism, Abstract class, Interface / Test Your Understanding - Abstract Class Started on Wednesday, 1 April 2020, 5:24 PM State Finished Completed on Wednesday, 1 April 2020, 5:29 PM Time taken 4 mins 19 secs Marks 7.00/8.00 Grade 87.50 out of 100.00 Feedback Congratulations!! You have passed by securing more than 80% Question An abstract class can have non abstract methods also. 1 Correct Mark 1.00 out of 1.00 Your answer is correct. The correct answer is: An abstract class [can] have non abstract methods also. Question 10. abstract public class Employee { 2 11. protected abstract double getSalesAmount(); Correct 12. public double getCommision() { Mark 1.00 out 13. return getSalesAmount() * 0.15; of 1.00 14. } 15. } 16. class Sales extends Employee { 17. protected double getSalesAmount() { return 1230.45; } 18. } Which method, inserted at line 17, correctly complete the Sales class? Since the Sales class is not abstract, it must have the implementation for the abstract method in Employee class. We must not reduce the visibility of the method in the child class. Hence, protected double getSalesAmount() { } The correct answer is: 10. abstract public class Employee { 11. protected abstract double getSalesAmount(); 12. public double getCommision() { 13. return getSalesAmount() * 0.15; 14. } 15. } 16. class Sales extends Employee { 17. [protected double getSalesAmount() { return 1230.45; } ] 18. } Which method, inserted at line 17, correctly complete the Sales class? Question Adeeb Khan. Abstract methods cannot be final. 3 Correct Mark 1.00 out of 1.00 Your answer is correct. The correct answer is: Abstract methods [cannot] be final. Question Adeeb Khan. What is the output of the given code? 4 abstract class Shape Correct { Mark 1.00 out int i = 111, j = 222; of 1.00 abstract void calcArea(); abstract void calcVolume(); } abstract class Quadrilateral extends Shape { void calcArea() { System.out.println(i); } } class Square extends Quadrilateral { void calcVolume() { System.out.println(j); } } public class Test { public static void main(String[] args) { Square c = new Square(); c.calcArea(); c.calcVolume(); } } Select one: a. Compile time error because trying to instantiate the 'class Square' which does not override all the abstract methods b. Compile time error because 'class Square' is not override all the abstract methods, so should declare it as 'abstract' c. Run time Error d. 111 222 Here is multilevel inheritance. The main method has object created for class Square, referred by Square type reference. The abstract methods clacArea() and calcVolume() in parent have their implementation within their children. Only when an implementation is not found child class, it is searched for in parent class and executed. Hence 111, 222. The correct answer is: 111 222 Question Adeeb Khan. Predict the output of the following program: 5 abstract class Demo Correct { Mark 1.00 out of 1.00 public int a; Demo() { a = 10; } abstract public void set(); abstract final public void get(); } class Test extends Demo { public void set(int a) { this.a = a; } final public void get() { System.out.println("a = " + a); } public static void main(String[] args) { Test obj = new Test(); obj.set(20); obj.get(); } } Select one: a. a = 20 b. a = 10 c. Compilation error Your answer is correct. The correct answer is: Compilation error Adeeb Khan. Question Will the following code get executed successfully ? 6 abstract class Shape Incorrect { Mark 0.00 out of 1.00 int i = 111, j = 222; abstract void calcArea(); abstract void calcVolume(); } abstract class Square extends Shape { void calcVolume() { System.out.println(j); } void calcArea(){ System.out.println(j); } } public class Test { public static void main(String[] args) { Square c = new Square(); c.calcArea(); c.calcVolume(); } } Select one: a. Yes, the code will get executed successfully. b. No – Compilation error. Object cannot be created for abstract class. The correct answer is: No – Compilation error. Question What is the Output of following Java Program? Adeeb Khan. 7 abstract class Demo Correct { Mark 1.00 out public int a; of 1.00 Demo() { a = 10; } abstract public void set(); } class Test extends Demo { final public void get() { System.out.println("a = " + a); } public static void main(String[] args) { Test obj = new Test(); obj.get(); } } Select one: a. Compile Time Error b. Runtime Exception c. a=10 The program produces compile time error because the abstract set method in the parent has not found its implementation in child class (which has the main method too). The correct answer is: Compile Time Error Question Will the below code will execute successfully ? 8 abstract class Shape Correct { Mark 1.00 out of 1.00 final abstract int calcArea(); } Select one: True False Since the abstract method within the abstract class is declared final, it can never be implemented in any of its child classes. Hence, it is an error. The correct answer is 'False'. 31/03/2020 Test Your Understanding - Inheritance: Attempt review Adeeb Khan. Dashboard / Java / Inheritance, Polymorphism, Abstract class, Interface / Test Your Understanding - Inheritance Started on Tuesday, 31 March 2020, 12:04 AM State Finished Completed on Tuesday, 31 March 2020, 12:27 AM Time taken 23 mins 15 secs Marks 9.67/12.00 Grade 80.56 out of 100.00 Feedback Congratulations!! You have passed by securing more than 80% Question 10. class FourWheeler { 1 11. protected void display() { } Incorrect Mark 0.00 out 12. } of 1.00 13. class Car extends FourWheeler { 14. private void display() { } 15. } Which method at line 14, will correctly complete class Car? The visibility of a method can only be increased as we traverse down the hierarchy. The correct answer is: 10. class FourWheeler { 11. [private] void display() { } 12. } 13. class Car extends FourWheeler { 14. [protected ] void display() { } 15. } Which method at line 14, will correctly complete class Car? Question Given a method in a public class, private access modifier must be used to restrict access to that method to only 2 the other members of the same class. Correct Mark 1.00 out of 1.00 Your answer is correct. The correct answer is: Given a method in a public class, [private] access modifier must be used to restrict access to that method to only the other members of the same class. https://accenturelearning.tekstac.com/mod/quiz/review.php?attempt=1035626&cmid=1024#q12 1/8 31/03/2020 Test Your Understanding - Inheritance: Attempt review Question Adeeb Khan. 3 Constructor of the superclass can be invoked by from its subclass. Correct A default member of a super class in one package cannot be accessed in its own subclass which is in a different Mark 1.00 out package. of 1.00 A protected member of a super class in one package can be accessed by its own subclass which is in a different package. Your answer is correct. The correct answer is: Constructor of the superclass [can] be invoked by from its subclass. A default member of a super class in one package [cannot] be accessed in its own subclass which is in a different package. A protected member of a super class in one package [can] be accessed by its own subclass which is in a different package. https://accenturelearning.tekstac.com/mod/quiz/review.php?attempt=1035626&cmid=1024#q12 2/8 31/03/2020 Test Your Understanding - Inheritance: Attempt review Question Adeeb Khan. What will be the output of the following program ? 4 class FourWheeler Correct { public FourWheeler() Mark 1.00 out { of 1.00 System.out.println("Class FourWheeler"); } } class Car extends FourWheeler { public Car() { System.out.println("Class Car"); } } class Audi extends Car { public Audi() { super(); System.out.println("Class Audi"); } } class Driver { public static void main(String args[]) { Audi cc=new Audi(); } } Select one: a. Exception occurs b. Class Audi Class Car Class FourWheeler c. Compile Time Error d. Class FourWheeler Class Car Class Audi The first statement that always gets executed from within any constructor is super() which means the invocation of super class no-parameterized constructor. FourWheeler is the parent of Car and Car is the parent of Audi. The no-parameterized constructor call of Audi happens from the driver class. Followed by the no-parameterized constructor call of Car. Followed by the no-parameterized constructor call of FourWheeler. Hence, the output "Class Four WheelerClass Car Class Audi". The correct answer is: Class FourWheeler Class Car Class Audi https://accenturelearning.tekstac.com/mod/quiz/review.php?attempt=1035626&cmid=1024#q12 3/8 31/03/2020 Test Your Understanding - Inheritance: Attempt review Question Say that there are three classes: Computer, AppleComputer, and IBMComputer. What are the likely relationships between Adeeb these Khan. 5 classes Correct Mark 1.00 out of 1.00 Select one: a. Computer, AppleComputer and IBMComputer are sibling classes. b. Computer is a superclass, AppleComputer is a subclasses of Computer, and IBMComputer is a sublclas of AppleComputer c. Computer is the super class, AppleComputer and IBMComputer are subclasses of computer d. IBMComputer is the superclass, AppleComputer and Computer are subclasses of IBMComputer. Your answer is correct. The correct answer is: Computer is the super class, AppleComputer and IBMComputer are subclasses of computer Question Given: 6 class FourWheeler Correct { public FourWheeler () Mark 1.00 out { of 1.00 System.out.print(1); } } class Car extends FourWheeler { public Car() { System.out.print(2); } } class Audi extends Car { public Audi() { System.out.print(3); } } public class Driver { public static void main( String[] argv ) { new Audi(); } } What is the result when this code is executed? Select one: a. The code runs with no output b. 321 c. 3 d. 123 The first statement that always gets executed from within any constructor is super() which means the invocation of super class no-parameterized constructor. FourWheeler is the parent of Car and Car is the parent of Audi. The no-parameterized constructor call of Audi happens from the driver class. Followed by the no-parameterized constructor call of Car. Followed by the no-parameterized constructor call of FourWheeler. Hence, the output "123". The correct answer is: 123 https://accenturelearning.tekstac.com/mod/quiz/review.php?attempt=1035626&cmid=1024#q12 4/8 31/03/2020 Test Your Understanding - Inheritance: Attempt review Adeeb Khan. Question Interpret which of the following statements are correct with respect to inheritance relationship in java? 7 Partially correct Mark 0.67 out Select one or more: of 1.00 a. object of subclass referenced by super class type can invoke overridden sub class methods b. object of subclass referenced by super class type can access super class variables c. object of subclass referenced by super class type can access newly defined sub class variables d. object of subclass referenced by super class type can invoke newly defined sub class methods e. object of subclass referenced by super class type can invoke super class methods Your answer is partially correct. You have correctly selected 2. The correct answers are: object of subclass referenced by super class type can invoke super class methods, object of subclass referenced by super class type can invoke overridden sub class methods, object of subclass referenced by super class type can access super class variables Question Default Access is the most restrictive access modifier that will allow members of one class to have access to members 8 of another class in the same package. Correct Mark 1.00 out of 1.00 Public Synchronized Protected Abstract Your answer is correct. The correct answer is: [Default Access] is the most restrictive access modifier that will allow members of one class to have access to members of another class in the same package. https://accenturelearning.tekstac.com/mod/quiz/review.php?attempt=1035626&cmid=1024#q12 5/8 31/03/2020 Test Your Understanding - Inheritance: Attempt review Question class FourWheeler{ Adeeb Khan. 9 protected FourWheeler getObject() { Incorrect //logic with return statement Mark 0.00 out } of 1.00 } class Car extends FourWheeler { protected void getObject() {} } class Driver { public static void main(String args[]) { FourWheeler object = new Car(); object.getObject(); } } private FourWheeler getObject() {} protected Car getObject() {} Car getObject() {} Java 5.0 onwards it is possible to have different return type for a overriding method in child class, but child’s return type should be sub-type of parent’s return type. The visibility of a method can only be increased as we traverse down the hierarchy. The correct answer is: class FourWheeler{ protected FourWheeler getObject() { //logic with return statement } } class Car extends FourWheeler { [protected Car getObject() {} ] } class Driver { public static void main(String args[]) { FourWheeler object = new Car(); object.getObject(); } } https://accenturelearning.tekstac.com/mod/quiz/review.php?attempt=1035626&cmid=1024#q12 6/8 31/03/2020 Test Your Understanding - Inheritance: Attempt review Question The class Employee is declared in a package mainpack and the Derived classes PermanentEmployee and Adeeb Khan. 10 TemporaryEmployee are declared in another package subordpack. The basicPay attribute should be accessed only by means Correct of a derived class object. Mark 1.00 out How to ensure that the basicPay attribute is not accessed directly by the other classes in the subordpack? of 1.00 Employee.java package mainpack ; public class Employee{ protected int basicPay; } package subordpack ; public class PermanentEmployee extends Employee{..... } //TemporaryEmployee.java package subordpack ; public class TemporaryEmployee extends Employee{..... } For a child class that is residing in a package to access a variable of its parent that is residing in a different package, the variable in the parent has to be declared "protected" so that it will be visible to all its children across packages. The "protected" basicPay attribute is not accessed directly by the other classes in the subordpack, in our case. The correct answer is: The class Employee is declared in a package mainpack and the Derived classes PermanentEmployee and TemporaryEmployee are declared in another package subordpack. The basicPay attribute should be accessed only by means of a derived class object. How to ensure that the basicPay attribute is not accessed directly by the other classes in the subordpack? Employee.java package [mainpack]; public class Employee{ [protected] int basicPay; } package [subordpack]; public class PermanentEmployee extends Employee{..... } //TemporaryEmployee.java package [subordpack]; public class TemporaryEmployee extends Employee{..... } Question State True or False 11 Child class objects can be instantiated when the parent class constructor is protected Correct Mark 1.00 out of 1.00 Select one: True False The correct answer is 'True'. https://accenturelearning.tekstac.com/mod/quiz/review.php?attempt=1035626&cmid=1024#q12 7/8 31/03/2020 Test Your Understanding - Inheritance: Attempt review Question Adeeb Khan. public class ArithmeticOperation{ 12 private void add(int operand1, int operand2) Correct { System.out.println(operand1 + operand2); Mark 1.00 out of 1.00 } } public class Addition extends ArithmeticOperation { public void show() { add(10, 12); } public static void main(String args[]) { Addition ob = new Addition(); ob.show(); } } What will be the output of above code when compiled and executed? Select one: a. Runtime error as add method is not defined in MethodOverriding class b. Will compile and display 32 c. Compile time error d. Will compile and display 1012 e. Will print false The method add cannot be invoked from anywhere since it's declared private The correct answer is: Compile time error https://accenturelearning.tekstac.com/mod/quiz/review.php?attempt=1035626&cmid=1024#q12 8/8 Adeeb Khan. Dashboard / Java / Inheritance, Polymorphism, Abstract class, Interface / Test Your Understanding - Interface Started on Saturday, 4 April 2020, 12:25 AM State Finished Completed on Saturday, 4 April 2020, 12:28 AM Time taken 3 mins 17 secs Marks 8.00/9.00 Grade 88.89 out of 100.00 Feedback Congratulations!! You have passed by securing more than 80% Question Predict the output 1 class Car implements Insurance Correct { Mark 1.00 out public int calcPremium(int i) of 1.00 { return i = i * i; } } interface Insurance { int calcPremium(int i); } public class MainClass { public static void main(String[] args) { Insurance b = new Car(); System.out.println(b.calcPremium(2)); } } Select one: a. Compile time error because you cannot create an object of type interface Insurance b. Run time Error c. The output will be 4 d. Compile time error because you must create interface before implementing it. b.calcPremium(2) will look for the method within the interface - b being the reference of interface type. The definition of this method is given in the Car class that has implemented this interface. With i=i*i, the output is 4. The correct answer is: The output will be 4 Question Predict the output. Adeeb Khan. 2 interface DoStuff2 Correct { float getRange(int low, int high); Mark 1.00 out } of 1.00 interface DoMore { float getAvg(int a, int b, int c); } abstract class DoAbstract implements DoStuff2, DoMore {} class DoStuff implements DoStuff2 { public float getRange(int x, int y) { return 3.14f; } } interface DoAll extends DoMore { float getAvg(int a, int b, int c, int d); } Select one: a. Compile time Error b. The file will compile without error. c. Runtime Error Your answer is correct. The correct answer is: The file will compile without error. Question The type Vehicle has drive functionality. The classes Car and Bike implements the drive functionality and can be further 3 subclassed. Fill in the given code with appropriate access specifier so that the subclasses of Car and Bike do not modify the Drive functionality. Correct Mark 1.00 out interface Vehicle{ of 1.00 void drive(); } class Car implements Vehicle{ public final void drive() { //drive } } Your answer is correct. The correct answer is: The type Vehicle has drive functionality. The classes Car and Bike implements the drive functionality and can be further subclassed. Fill in the given code with appropriate access specifier so that the subclasses of Car and Bike do not modify the Drive functionality. interface Vehicle{ void drive(); } class Car implements Vehicle{ [public] [final] void drive() { //drive } } Question Adeeb Khan. Predict the output 4 abstract class Vehicle Correct { Mark 1.00 out abstract void calcPremium(Number N); of 1.00 } interface Insurance { abstract void calcPremium (Object O); } class Car extends Vehicle implements Insurance { public void calcPremium (Object O) { System.out.println("Object"); } void calcPremium (Number N) { System.out.println("Number"); } } public class Test { public static void main(String[] args) { Vehicle a = new Car(); a. calcPremium (new Integer(121)); Insurance b = new Car(); b. calcPremium (new Integer(121)); Car c = new Car(); c. calcPremium (new Integer(121)); } } Select one: a. Number Object Number b. Number Number Object c. Run time error d. Compile time error a. calcPremium () with an integer object invokes this method within the Car class that takes Number argument. This is because a is the Vehicle type reference and Vehicle class has calcPremium () with a Number argument declared abstract. Hence, Number. b. calcPremium () with an integer object invokes this method within the Car class that takes Object argument. This is because b is the Insurance type reference and Insurance interface has calcPremium () with an Object argument declared abstract. Hence, Object. c. calcPremium () with an integer object invokes this method within the Car class that takes Number argument. Hence, Number. The correct answer is: Number Object Number Question Adeeb Khan. Predict the output: 5 interface Employee Correct { int a=90; Mark 1.00 out of 1.00 } class PermanentEmployee implements Employee { public void f1() { a=10; } } Select one: a. error, since interfaces Employee is not public b. error, since variable a is default c. error, since variable a is assigned a value d. no error Variables within interface are static and final by default. They can not be assigned a value in the classes that implement this interface. The correct answer is: error, since variable a is assigned a value Question If a class implements two interfaces and they both have a default method with same name and signature but different 6 implementation, then a conflict will arise because the compiler will not able to link a method call due to ambiguity. State true Incorrect or false. Mark 0.00 out of 1.00 Select one: True False The correct answer is 'True'. Question 11. public interface Status { 7 12. public static final double PI = 3.14; Correct 13. } Mark 1.00 out of 1.00 Fill the correct choice. Your answer is correct. The correct answer is: 11. public interface Status { 12. [public static final] double PI = 3.14; 13. } Fill the correct choice. Question public abstract interface Insurance{ Adeeb Khan. 8 public void insuranceDescription(String s); Correct } Which is the correct class? Mark 1.00 out of 1.00 Select one: a. public class Car implements Insurance { public void insuranceDescription (Integer i) { } } b. public abstract class Car implements Insurance { public abstract void insuranceDescription (String s) { } } c. public class Car extends Insurance { public void insuranceDescription (Integer i) { } } d. public abstract class Car implements Insurance { } Your answer is correct. The correct answer is: public abstract class Car implements Insurance { } Question 9 An interface can contain public, static, final fields (i.e., constants) default and static methods with bodies True Correct An instance of interface can be created. False Mark 1.00 out of 1.00 A class can implement multiple interfaces. True Many classes can implement the same interface. True Your answer is correct. The correct answer is: An interface can contain public, static, final fields (i.e., constants) default and static methods with bodies [True] An instance of interface can be created.[False] A class can implement multiple interfaces. [True] Many classes can implement the same interface.[True] Adeeb Khan. Dashboard / Java / Inheritance, Polymorphism, Abstract class, Interface / Test Your Understanding - Polymorphism Started on Wednesday, 1 April 2020, 4:19 PM State Finished Completed on Wednesday, 1 April 2020, 4:21 PM Time taken 2 mins 22 secs Marks 8.00/10.00 Grade 80.00 out of 100.00 Feedback Congratulations!! You have passed by securing more than 80% Question What will be the output of the following program ? 1 class A Correct { public void test() Mark 1.00 out of 1.00 { System.out.println("Class A"); } } public class Trial extends A { public void test() { System.out.println("Class Trial"); } public static void main(String args[]) { Trial object = (Trial)new A(); object.test(); } } Select one: a. Runtime Error b. Compile Time Error c. Class Trial d. Class A Trial object = (Trial)new A(); produces run time exception because a parent object can never be referred by a child type reference. The correct answer is: Runtime Error Question Adeeb Khan. PREDICT THE OUTPUT 2 Observe the following code Correct class FourWheeler Mark 1.00 out { of 1.00 public void display() { System.out.println("FourWheelers displayed"); } public void get() { System.out.println("Get FourWheelers"); display(); } } class Car extends FourWheeler { public void display() { System.out.println("Cars displayed"); super.display(); } public static void main(String [] args) { FourWheeler f=new Car(); f.get(); } } What is the output of the above code ? Select one: a. Get FourWheelers Cars displayed FourWheelers displayed b. Get FourWheelers FourWheelers displayed c. FourWheelers displayed Cars displayed d. Get FourWheelers FourWheelers displayed Cars displayed The main method has child object referred by parent reference. In this case, on invoking any method, only when a method is unavailable in the child, it is searched for in the parent. The functions get executed in the following order. get() of FourWheelers class display() of Car class diaplay() of FourWheelers class The correct answer is: Get FourWheelers Cars displayed FourWheelers displayed Question Adeeb Khan. Predict the output 3 Class Icecream{ Correct public void displayName(String...s){ Mark 1.00 out System.out.println(s+” “ +“Icecream"); of 1.00 } public void describe(String s) { System.out.println(s+” “ +"Icecream: Ice cream is a sweetened frozen food typically eaten as a snack or dessert. "); } } class Faloodeh extends Icecream { public void displayName (String s){ System.out.println(s+” “ +"Faloodeh "); } public void describe (String s) { System.out.println(s+” “ +"Faloodeh: Faloodeh is often served alongside Persian-style dairy-based ice cream "); } } public class Test { public static void main(String arg[]) { Icecream a=new Faloodeh (); Faloodeh b=( Faloodeh)a; a.displayName ("test"); b.displayName ("test"); a. describe ("test");b. describe ("test"); } } Select one: a. test Faloodeh: Faloodeh is often served alongside Persian-style dairy-based ice cream test Faloodeh test Faloodeh test Faloodeh: Faloodeh is often served alongside Persian-style dairy-based ice cream b. test Faloodeh test Faloodeh test Faloodeh: Faloodeh is often served alongside Persian-style dairy-based ice cream test Faloodeh: Faloodeh is often served alongside Persian-style dairy-based ice cream c. test Faloodeh test Faloodeh: Faloodeh is often served alongside Persian-style dairy-based ice cream test Faloodeh: Faloodeh is often served alongside Persian-style dairy-based ice cream test Faloodeh d. test Faloodeh: Faloodeh is often served alongside Persian-style dairy-based ice cream test Faloodeh: Faloodeh is often served alongside Persian-style dairy-based ice cream test Faloodeh test Faloodeh The main method has child object(Faloodeh) referred by parent reference(Icecream). Then the parent reference is downcasted to child type reference and assigned to the new child reference. On invoking displayName() with downcasted parent and child reference, the definition in child is implemented. On invoking describe() with downcasted parent and child reference, the definition in child is implemented. The correct answer is: test Faloodeh test Faloodeh test Faloodeh: Faloodeh is often served alongside Persian-style dairy-based ice cream test Faloodeh: Faloodeh is often served alongside Persian-style dairy-based ice cream Question Adeeb Khan. If you have - final class Test {} how to create a reference for the class “Test” while inheriting it? 4 Incorrect Mark 0.00 out of 1.00 Select one: a. Exception occurs b. Compilation Error : we can't inherit the class which is “final”. c. not necessary to create reference, automatically calls by the sub class reference. d. Test t=new Test(); Your answer is incorrect. The correct answer is: Compilation Error : we can't inherit the class which is “final”. Question What will be the output of the program? 5 class Tree { } class Pine extends Tree { } Correct class Oak extends Tree { } Mark 1.00 out public class Forest1 of 1.00 { public static void main (String [] args) { Tree tree = new Pine(); if( tree instanceof Pine ) System.out.println ("Pine"); else if( tree instanceof Tree ) System.out.println ("Tree"); else if( tree instanceof Oak ) System.out.println ( "Oak" ); else System.out.println ("Oops "); } } Select one: a. Tree b. Pine c. Forest d. Oops Since Pine object is held by Tree type reference - tree, the first condition in the if construct evalutes to true and hence, "Pine". The correct answer is: Pine Question The equals() method takes the reference of Object as parameter. State true or false. 6 Correct Mark 1.00 out Select one: of 1.00 True False The correct answer is 'True'. Question Adeeb Khan. 7 final methods can't be overridden. Correct Mark 1.00 out of 1.00 Your answer is correct. The correct answer is: [final methods] can't be overridden. Question Which three statements are true? 8 Not answered Select one or more: Marked out of a. A protected method in class X can be overridden by a subclass of A only if the subclass is in the same package as X. 1.00 b. A public static method in class X can be called by a subclass of X without explicitly referencing the class X. c. A non-static public final method in class X can be overridden in any subclass of X. d. A private static method can be called only within other static methods in class X. e. A method with the same signature as a private final method in class X can be implemented in a subclass of X. f. A final method in class X can be abstract if and only if X is abstract. g. A protected method in class X can be overridden by any subclass of X. Your answer is incorrect. The correct answers are: A protected method in class X can be overridden by any subclass of X., A public static method in class X can be called by a subclass of X without explicitly referencing the class X., A method with the same signature as a private final method in class X can be implemented in a subclass of X. Question class Calculator Adeeb Khan. 9 { Correct Calculator() Mark 1.00 out { of 1.00 System.out.println("Basic arithmetic operation "); } Calculator (int x) { this(); System.out.println(x + " " + "is the only operand supplied"); } Calculator(int x, int y) { this(5); System.out.println("Two operands supplied are multiplied and the resultant is "+ x * y); } public static void main(String args[]) { new Calculator(8, 10); } } Select one: a. Compilation error b. 5 is the only operand supplied Two operands supplied are multiplied