IT - III A

Approved & Edited by ProProfs Editorial Team
The editorial team at ProProfs Quizzes consists of a select group of subject experts, trivia writers, and quiz masters who have authored over 10,000 quizzes taken by more than 100 million users. This team includes our in-house seasoned quiz moderators and subject matter experts. Our editorial experts, spread across the world, are rigorously trained using our comprehensive guidelines to ensure that you receive the highest quality quizzes.
Learn about Our Editorial Process
| By Ankit
A
Ankit
Community Contributor
Quizzes Created: 1 | Total Attempts: 114
Questions: 35 | Attempts: 114

SettingsSettingsSettings
IT - III A - Quiz

Questions and Answers
  • 1. 

    Which of the following is true about inheritance in Java? 1) Private methods are final. 2) Protected members are accessible within a package and inherited classes outside the package. 3) Protected methods are final. 4) We cannot override private methods.

    • A.

      1,2 and 4

    • B.

      1 and 2 only

    • C.

      1,2 and 3

    • D.

      2,3 and 4

    Correct Answer
    A. 1,2 and 4
    Explanation
    Private methods in Java are not final. Final methods cannot be overridden, but private methods cannot be accessed or inherited by any subclass. Protected members are accessible within a package and to inherited classes outside the package. We cannot override private methods because they are not accessible to the subclass. Therefore, the correct answer is 1, 2, and 4.

    Rate this question:

  • 2. 

    By Default, members of a class can be accessed through its objects from

    • A.

      Within the same class

    • B.

      Within the same packege

    • C.

      Anywhere

    • D.

      None of the above

    Correct Answer
    B. Within the same packege
    Explanation
    Members of a class can be accessed through its objects from within the same package. This means that if a class is defined in a certain package, any other class within the same package can access its members using objects of that class. However, classes outside the package cannot access the members of the class directly. This is because access modifiers in Java, such as public, private, and protected, control the visibility and accessibility of class members.

    Rate this question:

  • 3. 

    What will be output? class T{ T() { super(); System.out.println("object"); } public static void main(String []args){ T t = new T(); } }

    • A.

      Compile time error no super class exists

    • B.

      Object

    • C.

      Compile successfully but no output

    • D.

      Runtime Error

    Correct Answer
    B. Object
    Explanation
    The given code is a class named T with a constructor. In the constructor, the super() method is called, which is used to invoke the constructor of the superclass. In this case, since the class T does not have any explicit superclass, the super() method is not necessary. The code then prints "object" to the console. In the main method, an object of class T is created, which triggers the constructor and the output "object" is printed. Therefore, the correct answer is "object".

    Rate this question:

  • 4. 

    What will be output? class A{ A(){System.out.println("class A");} } class B extends A{ B(){System.out.println("class B");} } class C extends B{ C(){System.out.println("class C");} } class Main { public static void main(String args[]){ B obj=new C(); } }

    • A.

      Class B

    • B.

      Class B class C

    • C.

      Class A class B class C

    • D.

      None of the mentioned

    Correct Answer
    C. Class A class B class C
    Explanation
    The output will be "class A class B class C". This is because the main method creates an object of class C and assigns it to a variable of type B. Since class C is a subclass of class B, this is allowed. When the object is created, the constructors of class A, class B, and class C are called in order. Therefore, the output is "class A class B class C".

    Rate this question:

  • 5. 

    A sub class does not inherits

    • A.

      All methods

    • B.

      All Variables

    • C.

      All Constructors

    • D.

      All of the above

    Correct Answer
    C. All Constructors
    Explanation
    A subclass does not inherit all constructors from its superclass. Constructors are special methods used to initialize objects of a class. Each class can have its own constructors, and they are not inherited by subclasses. Subclasses can have their own constructors, but they cannot directly access or use the constructors of their superclass. Therefore, the correct answer is "All Constructors".

    Rate this question:

  • 6. 

    In the following class, method declaration is? class test { void show(int …a) {} void show(int[] a){} }

    • A.

      Method overloaded

    • B.

      Method duplicated

    • C.

      Method hiding

    • D.

      None of the mentioned

    Correct Answer
    B. Method duplicated
    Explanation
    The correct answer is "Method duplicated" because the class "test" has two methods with the same name "show", but with different parameters. This is known as method overloading, where multiple methods can have the same name but with different parameters. In this case, the two methods have different parameter types - one accepts a variable number of integers and the other accepts an array of integers.

    Rate this question:

  • 7. 

    Super keyword in java is used to ?

    • A.

      Refer to parent class object.

    • B.

      Refer to static method of the class.

    • C.

      Refer to current class object.

    • D.

      Refer to static variable of the class.

    Correct Answer
    A. Refer to parent class object.
    Explanation
    The super keyword in Java is used to refer to the parent class object. It is often used to access the parent class's members (variables and methods) that have been hidden or overridden by the child class. This allows for the implementation of inheritance and facilitates code reuse and polymorphism.

    Rate this question:

  • 8. 

    What will be output? class Hotel { public int bookings; public void book() { bookings++; } } public class SuperHotel extends Hotel { public void book() { bookings--; } public void book(int size) { book(); super.book(); bookings += size; } public static void main(String args[]) { SuperHotel hotel = new SuperHotel(); hotel.book(2); System.out.print(hotel.bookings); } }

    • A.

      0

    • B.

      4

    • C.

      1

    • D.

      2

    Correct Answer
    D. 2
    Explanation
    The output will be 2. In the main method, a new SuperHotel object is created. Then the book(int size) method is called with a size of 2. Inside the book(int size) method, the book() method is called, which decreases the bookings by 1. Then the super.book() method is called, which increases the bookings by 1. Finally, the size is added to the bookings. Since the initial value of bookings is 0, the final value will be 2.

    Rate this question:

  • 9. 

    What will be output? class Vehicle { public void printSound() { System.out.print("vehicle"); } } class Car extends Vehicle { public void printSound() { System.out.print("car"); } } class Bike extends Vehicle { public void printSound() { System.out.print("bike"); } } public class Test { public static void main(String[] args) { Vehicle v = new Car(); Bike b = (Bike) v; v.printSound(); b.printSound(); } }

    • A.

      Compilation fails.

    • B.

      An exception is thrown at runtime.

    • C.

      "vehiclecar" is printed.

    • D.

      "vehiclebike" is printed.

    Correct Answer
    B. An exception is thrown at runtime.
    Explanation
    The code is trying to cast a Car object to a Bike object, which is not possible because Car is a subclass of Vehicle and Bike is also a subclass of Vehicle. This will result in a ClassCastException at runtime.

    Rate this question:

  • 10. 

    What will be output? class Num { Num(double x ) { System.out.println( x ) ; } } class Numbers extends Num { public static void main(String[] args) { Num num = new Num( 2 ) ; } }

    • A.

      "2"

    • B.

      "2.0"

    • C.

      Compile time error

    • D.

      None of the mentioned

    Correct Answer
    B. "2.0"
    Explanation
    The output will be "2.0" because the constructor in the Num class takes a double parameter, and when the object is created in the Numbers class, the value 2 is passed as an argument. Since 2 is an integer literal, it is automatically promoted to a double value of 2.0. Therefore, the output will be 2.0.

    Rate this question:

  • 11. 

    In genereal the order of the three top level elements of the java source file are

    • A.

      Import, Package, Class

    • B.

      Import, Class, Package

    • C.

      Package, Import, Class

    • D.

      Random order

    Correct Answer
    C. Package, Import, Class
    Explanation
    The correct order of the three top-level elements in a Java source file is Package, Import, Class. The package statement is used to define the package that the file belongs to. The import statements are used to import external classes or packages that are needed in the file. Finally, the class declaration is used to define the main class in the file.

    Rate this question:

  • 12. 

    Object-oriented inheritance models the

    • A.

      "is a ” relationship

    • B.

      “has a” relationship

    • C.

      “want to be” relationship

    • D.

      inheritance does not describe any kind of relationship between classes

    Correct Answer
    A. "is a ” relationship
    Explanation
    Object-oriented inheritance models the "is a" relationship. Inheritance allows a class to inherit the properties and behaviors of another class, creating a hierarchy where a subclass is a specialized version of its superclass. This relationship implies that the subclass shares common characteristics with the superclass but may also have additional attributes or behaviors specific to itself. By using inheritance, code reuse and abstraction can be achieved, making it easier to organize and maintain complex software systems.

    Rate this question:

  • 13. 

    The concept of multiple inheritances is implemented in Java by I. Extending two or more classes. II. Extending one class and implementing one or more interfaces. III. Implementing two or more interfaces.

    • A.

      Only II

    • B.

      I and II

    • C.

      II and III

    • D.

      Only III

    Correct Answer
    C. II and III
    Explanation
    The correct answer is II and III. In Java, multiple inheritances can be achieved by extending one class and implementing one or more interfaces (II) or by implementing two or more interfaces (III). Java does not support extending two or more classes (I) due to the issue of diamond inheritance problem. This problem occurs when a class inherits from two classes that have a common superclass, causing ambiguity. So, the correct way to achieve multiple inheritances in Java is through the use of interfaces.

    Rate this question:

  • 14. 

    What is the error in the following class definitions? abstract class Math { abstract sum (int x, int y) { } }

    • A.

      Class name is not defined properly

    • B.

      Constructor is not defined.

    • C.

      Method is not defined properly

    • D.

      No error.

    Correct Answer
    C. Method is not defined properly
    Explanation
    The error in the given class definitions is that the method "sum" is not defined properly. In an abstract class, abstract methods should have a return type specified. In this case, the return type of the "sum" method is missing, which is incorrect.

    Rate this question:

  • 15. 

    Which of these field declarations are legal within the body of an interface?

    • A.

      Private final static int answer = 42

    • B.

      Public static int answer=42

    • C.

      Final static answer =42

    • D.

      Int answers

    Correct Answer
    B. Public static int answer=42
    Explanation
    The correct answer is "public static int answer=42". In an interface, all fields are implicitly public, static, and final. Therefore, the field declaration "public static int answer=42" is legal within the body of an interface. The other options are not legal because they either have incorrect access modifiers (private) or are missing the data type (final static answer =42) or are missing a semicolon (int answers).

    Rate this question:

  • 16. 

    . Which of the three keywords are valid when preceeding the keyword int in the following code? public interface JavaQuiz { int variable = 1000; }

    • A.

      Public , static and final

    • B.

      Private, transient and abstract

    • C.

      Public , transient and abstract

    • D.

      Protected, final and static

    Correct Answer
    A. Public , static and final
    Explanation
    The three valid keywords that can precede the keyword int in the given code are public, static, and final. These keywords are used to define the accessibility, behavior, and immutability of the variable. The public keyword allows the variable to be accessed from anywhere, the static keyword makes the variable shared among all instances of the class, and the final keyword makes the variable unchangeable once it is assigned a value.

    Rate this question:

  • 17. 

    Which of these can be used to fully abstract a class from its implementation?

    • A.

      Objects

    • B.

      Packages

    • C.

      Interfaces

    • D.

      None of the listed.

    Correct Answer
    C. Interfaces
    Explanation
    Interfaces can be used to fully abstract a class from its implementation. This is because an interface defines a contract or a set of methods that a class must implement. By using interfaces, the implementation details of a class are hidden, allowing different classes to provide their own implementation of the interface methods. This promotes loose coupling and allows for easier maintenance and flexibility in the code.

    Rate this question:

  • 18. 

    What is the output of this program? interface calculate { void cal(int item); } class display implements calculate { int x; public void cal(int item) { x = item * item; } } class interfaces { public static void main(String args[]) { display arr = new display(); arr.x = 0; arr.cal(2); System.out.print(arr.x); } }

    • A.

      0

    • B.

      2

    • C.

      4

    • D.

      None of the listed.

    Correct Answer
    C. 4
    Explanation
    The output of this program is 4.
    In the main method, an object of the display class is created and assigned to the variable 'arr'. The variable 'x' is then initialized to 0.
    The cal method is called on the 'arr' object with the argument 2. Inside the cal method, the value of 'x' is calculated by multiplying the 'item' parameter with itself (2 * 2 = 4).
    Finally, the value of 'x' (which is 4) is printed using System.out.print.

    Rate this question:

  • 19. 

    Which of these access specifiers can be used for an interface?

    • A.

      Public

    • B.

      Private

    • C.

      Protected

    • D.

      All of the above

    Correct Answer
    A. Public
    Explanation
    The access specifier "Public" can be used for an interface. This means that the interface can be accessed by any class or code in the program. "Private" and "Protected" access specifiers cannot be used for an interface. "Private" restricts access to only within the same class, while "Protected" allows access within the same class and its subclasses. Therefore, the correct answer is "Public".

    Rate this question:

  • 20. 

    Given the following piece of code: public interface Guard{ void doYourJob(); } abstract public class Dog implements Guard{ } which of the following statements is correct?

    • A.

      This code will not compile, because method doYourJob() in interface Guard must be defined abstract.

    • B.

      This code will not compile, because class Dog must implement method doYourJob() from interface Guard.

    • C.

      This code will not compile, because in the declaration of class Dog we must use the keyword extends instead of implements.

    • D.

      This code will compile without any errors.

    Correct Answer
    D. This code will compile without any errors.
    Explanation
    The code will compile without any errors because the class Dog is declared as abstract and it inherits the method doYourJob() from the interface Guard. Since it is an abstract class, it is not required to implement the method.

    Rate this question:

  • 21. 

    Which of the following is FALSE about abstract classes in Java

    • A.

      If we derive an abstract class and do not implement all the abstract methods, then the derived class should also be marked as abstract using 'abstract' keyword

    • B.

      Abstract classes can have constructors

    • C.

      A class can be made abstract without any abstract method

    • D.

      A class can inherit from multiple abstract classes

    Correct Answer
    D. A class can inherit from multiple abstract classes
    Explanation
    In Java, a class can only inherit from one superclass, whether it is abstract or not. Multiple inheritance is not allowed in Java. Therefore, the statement that "A class can inherit from multiple abstract classes" is false.

    Rate this question:

  • 22. 

    Which of the following is true about interfaces in java. 1) An interface can contain following type of members. ....public, static, final fields (i.e., constants) ....default and static methods with bodies 2) An instance of interface can be created. 3) A class can implement multiple interfaces. 4) Many classes can implement the same interface.

    • A.

      1, 3 and 4

    • B.

      1, 2 and 4

    • C.

      2, 3 and 4

    • D.

      1, 2, 3 and 4

    Correct Answer
    A. 1, 3 and 4
    Explanation
    The correct answer is 1, 3 and 4.


    1) An interface in Java can contain public, static, and final fields (constants), as well as default and static methods with bodies. This allows interfaces to provide constants and default implementations for methods.

    3) A class in Java can implement multiple interfaces. This allows a class to inherit and implement multiple sets of behaviors from different interfaces.

    4) Many classes in Java can implement the same interface. This allows for polymorphism and allows different classes to be used interchangeably when they implement the same interface.

    Rate this question:

  • 23. 

    Predict the output of the following program. abstract class demo { 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(); } }

    • A.

      A = 10

    • B.

      A = 20

    • C.

      Compilation error

    • D.

      Runtime Error

    Correct Answer
    C. Compilation error
    Explanation
    The program will produce a compilation error. This is because the method "set(int a)" in the Test class does not match the abstract method "set()" in the demo class. The method in the Test class has a parameter, while the abstract method in the demo class does not have any parameters. Therefore, the program cannot be compiled successfully.

    Rate this question:

  • 24. 

    Which of these packages contains abstract keyword?

    • A.

      Java.lang

    • B.

      Java.util

    • C.

      Java.io

    • D.

      Java.system

    Correct Answer
    A. Java.lang
    Explanation
    The correct answer is java.lang. The java.lang package is a core package in Java that contains fundamental classes and interfaces. The abstract keyword is used to declare abstract classes and methods, and it is commonly found in the java.lang package.

    Rate this question:

  • 25. 

    Predict the output abstract class A { int i; abstract void display(); } class B extends A { int j; void display() { System.out.println(j); } } class Abstract_demo { public static void main(String args[]) { B obj = new B(); obj.j=2; obj.display(); } }

    • A.

      0

    • B.

      2

    • C.

      Runtime Error

    • D.

      Compile Time Error

    Correct Answer
    B. 2
    Explanation
    The code snippet defines an abstract class A with an integer variable i and an abstract method display(). Class B extends class A and overrides the display() method to print the value of j. In the main method, an object of class B is created and the value of j is set to 2. The display() method is then called, which prints the value of j, which is 2. Therefore, the output of the code is 2.

    Rate this question:

  • 26. 

    If a class inheriting an abstract class does not define all of its function then it will be known as?

    • A.

      Abstract

    • B.

      A simple class

    • C.

      Static class

    • D.

      None of the mentioned

    Correct Answer
    A. Abstract
    Explanation
    If a class inheriting an abstract class does not define all of its functions, it will be known as an abstract class. An abstract class is a class that cannot be instantiated and is meant to be inherited by other classes. It contains one or more abstract methods, which are methods without a body. These abstract methods must be defined by any class that inherits from the abstract class, otherwise, that class will also be considered abstract. Therefore, if a class inheriting an abstract class does not define all of its functions, it will be known as an abstract class.

    Rate this question:

  • 27. 

    Which of these class is not a member class of java.io package?

    • A.

      String

    • B.

      StringReader

    • C.

      Writer

    • D.

      File

    Correct Answer
    A. String
    Explanation
    The class "String" is not a member class of the java.io package. The java.io package mainly deals with input and output operations, whereas the String class is part of the java.lang package and is used to manipulate strings. Therefore, it is not included in the java.io package.

    Rate this question:

  • 28. 

    Which of these classes is used for input and output operation when working with bytes?

    • A.

      InputStream

    • B.

      Reader

    • C.

      Writer

    • D.

      None of the mentioned

    Correct Answer
    A. InputStream
    Explanation
    InputStream is the correct answer because it is the base class for all input streams in Java, and it is used for input operations when working with bytes. It provides methods to read bytes from a source, such as a file or network connection. InputStream is commonly used for reading binary data, such as images or audio files, where the data is represented as a sequence of bytes. Reader and Writer classes, on the other hand, are used for character-based input and output operations, not specifically for working with bytes.

    Rate this question:

  • 29. 

    Given the following piece of code: public class Company{ public abstract double calculateSalaries(); } which of the following statements is true?

    • A.

      The keywords public and abstract can not be used together.

    • B.

      The method calculateSalaries() in class Company must have a body

    • C.

      You must add a return statement in method calculateSalaries().

    • D.

      Class Company must be defined abstract.

    Correct Answer
    D. Class Company must be defined abstract.
    Explanation
    The correct answer is that Class Company must be defined abstract. This is because the keyword "abstract" is used in the class declaration, indicating that the class itself is abstract. An abstract class cannot be instantiated and is typically used as a base class for other classes to inherit from. In this case, the abstract class Company provides a blueprint for other classes to implement the calculateSalaries() method.

    Rate this question:

  • 30. 

    Public interface Guard{ void doYourJob(); } abstract public class Dog implements Guard{} which of the following statements is correct?

    • A.

      This code will not compile, because method doYourJob() in interface Guard must be defined abstract.

    • B.

      This code will not compile, because class Dog must implement method doYourJob() from interface Guard.

    • C.

      This code will not compile, because in the declaration of class Dog we must use the keyword extends in stead of implements.

    • D.

      This code will compile without any errors.

    Correct Answer
    D. This code will compile without any errors.
    Explanation
    The code will compile without any errors because the class Dog is implementing the interface Guard. Since the interface does not have any abstract methods, the class Dog does not need to implement any methods.

    Rate this question:

  • 31. 

    Public class Person{ public void talk(){ System.out.print("I am a Person "); } } public class Student extends Person { public void talk(){ System.out.print("I am a Student "); } } what is the result of this piece of code: public class Test{ public static void main(String args[]){ Person p = new Student(); p.talk(); }}

    • A.

      I am a Person

    • B.

      I am a Student

    • C.

      I am a Person I am a Student

    • D.

      I am a Student I am a Person

    Correct Answer
    B. I am a Student
    Explanation
    The code creates a new object of the Student class and assigns it to a variable of type Person. Since the talk() method is overridden in the Student class, when the talk() method is called on the object, it prints "I am a Student". Therefore, the result of the code is "I am a Student".

    Rate this question:

  • 32. 

    Which of these keywords can be used to prevent Method overriding?

    • A.

      Static

    • B.

      Constant

    • C.

      Protected

    • D.

      Final

    Correct Answer
    D. Final
    Explanation
    The keyword "final" can be used to prevent method overriding. When a method is declared as final in a superclass, it cannot be overridden by any subclass. This means that the subclass cannot provide a different implementation of the method. By using the final keyword, the superclass ensures that the method remains unchanged and cannot be modified in any way by its subclasses.

    Rate this question:

  • 33. 

    What is the output of the program final class A { int i; } class B extends A { int j; System.out.println(j + " " + i); } class inheritance { public static void main(String args[]) { B obj = new B(); obj.display(); } }

    • A.

      2 2

    • B.

      3 3

    • C.

      Runtime error

    • D.

      Compilation error

    Correct Answer
    D. Compilation error
    Explanation
    The program will result in a compilation error because the class B is trying to access the variable 'i' from its superclass A, which is not allowed because 'i' is declared as private in class A. To access the variable 'i' in class B, it should be declared as protected or public in class A.

    Rate this question:

  • 34. 

    What will be output? class Bike{ void run(){System.out.println("running");} } class Splender extends Bike{ void run(){System.out.println("running safely with 60km");} public static void main(String args[]){ Bike b = new Splender(); b.run(); } }

    • A.

      Running

    • B.

      Runtime error

    • C.

      Running safely with 60km

    • D.

      Compile time erro

    Correct Answer
    C. Running safely with 60km
    Explanation
    The output will be "running safely with 60km" because the main method creates an instance of the Splender class and assigns it to a reference variable of type Bike. When the run() method is called on this reference variable, it invokes the overridden run() method in the Splender class, which prints "running safely with 60km".

    Rate this question:

  • 35. 

    What will be output? public class Animal{ public void sound(){ System.out.println("Animal is making a sound"); } } class Horse extends Animal{ public void sound(){ System.out.println("Neigh"); } public static void main(String args[]){ Horse obj = new Animal(); obj.sound(); } }

    • A.

      Neigh

    • B.

      Animal is making a sound

    • C.

      Compilation Error

    • D.

      Runtime Error

    Correct Answer
    C. Compilation Error
    Explanation
    The given code will result in a compilation error. This is because the object "obj" is declared as a Horse type, but it is being assigned a new instance of the Animal class. Since Horse is a subclass of Animal, it can be assigned to a variable of type Animal. However, the opposite is not true. In this case, the sound() method is overridden in the Horse class, but since the object is of type Animal, it does not have access to the overridden method. Therefore, the code will not compile.

    Rate this question:

Quiz Review Timeline +

Our quizzes are rigorously reviewed, monitored and continuously updated by our expert board to maintain accuracy, relevance, and timeliness.

  • Current Version
  • Mar 21, 2023
    Quiz Edited by
    ProProfs Editorial Team
  • Oct 11, 2019
    Quiz Created by
    Ankit
Back to Top Back to top
Advertisement