Podcast Beta
Questions and Answers
What will be the output of the program? public class Test { public static void main(String[] args) { int[] arr1 = {11,12,13,14,14,15}; int[] arr2 = arr1; arr1 = new int; for(int i = 0; i < arr1.length; i++) System.out.print(arr2[i] + ' '); } }
How many String objects will be created when this method is invoked? public String makingStrings() { String str = 'Welcome'; str = str.substring(1, 5); str = str.toUpperCase(); return str; }
Predict the output: public abstract class Abs { public Abs(){ System.out.println('Constructor from Abstract class'); } } public class Test extends Abs { public static void main(String args[]){ Abs obj=new Test(); } }
What is the result of the following code snippet? public static void test(String str) { if (str == null || str.length() == 0) { System.out.println('String is empty'); } else { System.out.println('String is not empty'); } } public static void main(String a[]) { test(null); }
Signup and view all the answers
To copy elements from one array to another array, Java provides efficient built-in methods in ________ class.
Signup and view all the answers
In Java, if you try to compile the below code: class MyStringDemo extends String { }
Signup and view all the answers
What will be the output of the code snippet? public class StringDemo { public static void main(String[]args) { String s1 = new String('java'); String s2 = new String('java'); System.out.println(s2.compareTo(s1)); } }
Signup and view all the answers
What will be the output of the program? public class Test { public static void main (String args[]) { String str = NULL; System.out.println(str); } }
Signup and view all the answers
What can be the valid access specifier for the calculate method in Calculator class so that it can be accessed from CalculatorService class?
Signup and view all the answers
What is required at line 4 in class Library to use the countBook method of Book class?
Signup and view all the answers
Rearrange this code correctly.
Signup and view all the answers
In main to create an object of Book, which of the following are valid?
Signup and view all the answers
Which two pairs of method declarations follow the JavaBeans standard for accessing the field 'enabled'?
Signup and view all the answers
Predict the Output of the following Java Program.
Signup and view all the answers
Identify the true statement(s) about constructors.
Signup and view all the answers
Identify which statement is true about constructors.
Signup and view all the answers
What is the result of the given Java program?
Signup and view all the answers
Predict the output: public class Trial { public static void main(String[] args) { int arr={ }; System.out.print(arr); } }
Signup and view all the answers
What will be the output of the above code snippet? int arr[]={12,4,22,5,1,66}; int position = Arrays.binarySearch(arr,5); System.out.println(position);
Signup and view all the answers
Abstract methods cannot be final.
Signup and view all the answers
An abstract class can have non-abstract methods also.
Signup and view all the answers
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(); }
Signup and view all the answers
Will the code below execute successfully?
Signup and view all the answers
What is the output of following Java Program? abstract class Demo { public int a; 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(); }
Signup and view all the answers
Will the below code execute successfully?
Signup and view all the answers
Which method at line 14 will correctly complete class Car?
Signup and view all the answers
Given a method in a public class, a private access modifier must be used to restrict access to that method to only other members of the same class.
Signup and view all the answers
What will be the output of the following program?
Signup and view all the answers
Say that there are three classes: Computer, AppleComputer, and IBMComputer. What are the likely relationships between these classes?
Signup and view all the answers
What is the result when the given code is executed?
Signup and view all the answers
Which members of a class can be accessed by other classes is determined by the ________________
Signup and view all the answers
What does this() mean in constructor chaining concept?
Signup and view all the answers
Match the following programming languages with their primary usage:
Signup and view all the answers
Which of the following statements regarding superclass and subclass types is true?
Signup and view all the answers
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.
Signup and view all the answers
In Java, what happens when a child class is declared in a different package and needs to access a parent class variable?
Signup and view all the answers
Child class objects can be instantiated when the parent class constructor is protected.
Signup and view all the answers
What will be the output of the following Java code snippet?
Signup and view all the answers
Predict the output. What will be the output of the following program?
Signup and view all the answers
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.
Signup and view all the answers
Predict the output. What is the output of the given code?
Signup and view all the answers
Predict the output: Interface Employee. Class PermanentEmployee implements Employee. What error will occur when executing the provided code?
Signup and view all the answers
An instance of an interface can be created.
Signup and view all the answers
What is the output of the above code?
Signup and view all the answers
If you have 'final class Test {}', how to create a reference for the class Test while inheriting it?
Signup and view all the answers
What will be the output of the program?
Signup and view all the answers
The equals() method takes the reference of Object as a parameter.
Signup and view all the answers
Final methods can/can't be overridden.
Signup and view all the answers
Which three statements are true?
Signup and view all the answers
What is the output of the 'Calculator' class code snippet?
Signup and view all the answers
Which members of a class can be accessed by other classes is determined by the ________________
Signup and view all the answers
What does this() mean in constructor chaining concept?
Signup and view all the answers
Match the following programming concepts:
Signup and view all the answers
Predict the output. What will be the output of the following program?
Signup and view all the answers
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.
Signup and view all the answers
What will be the output of the given code snippet?
Signup and view all the answers
An instance of an interface can be created.
Signup and view all the answers
A class can implement multiple interfaces.
Signup and view all the answers
What is the correct access specifier for the given class definition: public ______ class Car implements Insurance { }
Signup and view all the answers
If a class implements two interfaces with default methods of the same name and signature but different implementations, a conflict will arise due to ambiguity.
Signup and view all the answers
What will be the output of the following program?
Signup and view all the answers
Study Notes
Packages and Access Specifiers
- A method in one package can be accessed from another package if it is declared public.
- If a method is declared private, it cannot be accessed from outside the class.
- The correct access specifier for the calculate method in the Calculator class is public.
Importing Classes
- To use a class from another package, it needs to be imported.
- The correct statement to import the static variable PI is
import static java.lang.Math.PI;
.
JavaBeans Standard
- When writing getters and setters, setters return type is void and getters return type is the corresponding data type.
- The naming convention for getters and setters is camelcase notation.
- For boolean return type, it should start with 'is' or 'are' followed by the field name.
Constructors
- When no constructor is written in a class, the compiler creates a default constructor.
- The default constructor will implicitly invoke the default/no-argument constructor of the superclass.
- If a class has a parameterized constructor alone, then the compiler will not create a default constructor.
- Constructors can be overloaded.
Java Program Output
- In the given Java program, there is a compile-time error because the instance variable x is accessed from a static context.
Constructors Chaining
-
this()
is used to call the no-argument constructor of the same class.
Inheritance and Polymorphism
- Upcasting is when a subclass object is assigned to a superclass reference.
- Downcasting is when a superclass reference is cast to a subclass object.
Abstract Classes and Interfaces
- An abstract class can have both abstract and non-abstract methods.
- A default method in an interface can only be public.
- An abstract class can be declared as final if it should not be subclassed.
- If a class inherits an abstract class, it should provide an implementation for all the abstract methods in the parent class.
- The
super
keyword is used to invoke the overridden method in the superclass.### Inheritance and Polymorphism - Code reusability and runtime polymorphism can be achieved through inheritance.
- A method in an interface, when implemented, should be either static or default.
Lambda Expressions
- A correct lambda expression for the Functional method
int findMax(int a, int b)
is(int a, int b) -> { int min = a > b ? a : b; return min; }
.
Abstract Classes
- An abstract class can have non-abstract methods.
- An abstract class cannot be instantiated.
- In the constructor of a child class, the first line should be a call to the superclass constructor. If not written, then implicitly it invokes the superclass constructor as
super()
.
Constructors and Inheritance
- When a child class is instantiated, the constructor of the abstract class is called.
- The output of the code
class A { 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()); } }
isclass A has name A
andclass B has name A
.
String Methods
- The
toUpperCase()
method converts a string to uppercase. - The
substring()
method returns a new string that is a subset of the original string. - The
equals()
method checks if two strings are equal. - The
Arrays.equals()
method checks if two arrays are equal.
Arrays and String
- When an array is assigned to another array, it does not create a new copy of the array. Instead, it references the same array.
- The
Arrays.equals()
method can be used to compare two arrays.
Compilation and Runtime Errors
- A compile-time error occurs when the code does not compile due to syntax errors.
- A runtime error occurs when the code compiles but throws an exception at runtime.
Binary Search
- The
Arrays.binarySearch()
method can be used to search for an element in a sorted array. - If the array is not sorted, the
Arrays.binarySearch()
method will not work correctly.
Method Overloading and Overriding
- Method overriding is when a subclass provides a different implementation of a method that is already defined in its superclass.
- Method overloading is when multiple methods with the same name can be defined, but with different parameter lists.### Inheritance in Java
- Abstract methods cannot be final.
- Protected methods in a superclass cannot be made private in a subclass.
- An abstract class can have both abstract and non-abstract methods.
- Abstract methods must be implemented by a subclass.
- A subclass can extend only one abstract class.
- A subclass can implement multiple interfaces.
Method Overriding
- When a subclass provides a specific implementation for a method that is already defined in its superclass, it is called method overriding.
- The method in the subclass must have the same name, return type, and parameter list as the method in the superclass.
Method Hiding
- When a subclass provides a method with the same name as a method in its superclass, but with a different parameter list, it is called method hiding.
- The method in the subclass hides the method in the superclass, but it can still be accessed by using the super keyword.
Constructors and Inheritance
- A subclass constructor must call the superclass constructor using the super keyword.
- If a subclass does not explicitly call the superclass constructor, the compiler will automatically insert a call to the no-argument constructor of the superclass.
Access Modifiers and Inheritance
- A protected member of a superclass can be accessed by its subclass, even if it is in a different package.
- A default member of a superclass in one package cannot be accessed by its subclass in a different package, unless it is declared as protected.
Inheritance and Constructors
- When a subclass is created, its constructor is called, which in turn calls the constructor of its superclass.
- The order of constructor calls is from the top-level class to the bottom-level class.
Multiple Inheritance
- Java does not support multiple inheritance, where a subclass can extend multiple classes.
- However, a subclass can implement multiple interfaces.
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.
Related Documents
Description
This quiz covers advanced Java concepts including abstract classes, string manipulation, and array operations.