11-20 Flashcards

1
Q

What is ternary operator? Give an example.

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How can you generate random numbers in Java?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is default switch case?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What’s the base class in Java from which all classes are derived?

A

java.lang.object

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Can main() method in Java can return any data?

A

In java, main() method can’t return any data and hence, it’s always declared with a void return type.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are Java Packages? What’s the significance of packages?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Can we declare a class as Abstract without having any abstract method?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What’s the difference between an Abstract Class and Interface in Java?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What are the performance implications of Interfaces over abstract classes?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Does Importing a package imports its sub-packages as well in Java?

A

In java, when a package is imported, its sub-packages aren’t imported and developer needs to import them separately if required.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly