11-20 Flashcards
What is ternary operator? Give an example.
Ternary operator , also called conditional operator is used to decide which value to assign to a variable based on a Boolean value evaluation. It’s denoted as ?
status = (rank == 1) ? “Done” : “Pending”;
How can you generate random numbers in Java?
Using Math.random() you can generate random numbers in the range greater than or equal to 0.1 and less than 1.0
Using Random class in package java.util
What is default switch case?
In a switch statement, default case is executed when no other switch condition matches. Default case is an optional case .It can be declared only once all other switch cases have been coded.
What’s the base class in Java from which all classes are derived?
java.lang.object
Can main() method in Java can return any data?
In java, main() method can’t return any data and hence, it’s always declared with a void return type.
What are Java Packages? What’s the significance of packages?
In Java, package is a collection of classes and interfaces which are bundled together as they are related to each other. Use of packages helps developers to modularize the code and group the code for proper re-use. Once code has been packaged in Packages, it can be imported in other classes and used.
Can we declare a class as Abstract without having any abstract method?
Yes we can create an abstract class by using abstract keyword before class name even if it doesn’t have any abstract method. However, if a class has even one abstract method, it must be declared as abstract otherwise it will give an error.
What’s the difference between an Abstract Class and Interface in Java?
The primary difference between an abstract class and interface is that an interface can only possess declaration of public static methods with no concrete implementation while an abstract class can have members with any access specifiers (public, private etc) with or without concrete implementation.
Another key difference in the use of abstract classes and interfaces is that a class which implements an interface must implement all the methods of the interface while a class which inherits from an abstract class doesn’t require implementation of all the methods of its super class.
A class can implement multiple interfaces but it can extend only one abstract class.
What are the performance implications of Interfaces over abstract classes?
Interfaces are slower in performance as compared to abstract classes as extra indirections are required for interfaces. Another key factor for developers to take into consideration is that any class can extend only one abstract class while a class can implement many interfaces.
Use of interfaces also puts an extra burden on the developers as any time an interface is implemented in a class; developer is forced to implement each and every method of interface.
Does Importing a package imports its sub-packages as well in Java?
In java, when a package is imported, its sub-packages aren’t imported and developer needs to import them separately if required.