Interview 1 Flashcards

(14 cards)

1
Q

What’s the purpose of Static methods and static variables?

A

Ans: When there is a requirement to share a method or a variable between multiple objects of a class instead of creating separate copies for each object, we use static keyword to make a method or variable shared for all objects.

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

What is data encapsulation and what’s its significance?

A

Ans: Encapsulation is a concept in Object Oriented Programming for combining properties and methods in a single unit.

Encapsulation helps programmers to follow a modular approach for software development as each object has its own set of methods and variables and serves its functions independent of other objects. Encapsulation also serves data hiding purpose.

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

What is the difference between continue and break statement?

A

break and continue are two important keywords used in Loops. When a break keyword is used in a loop, loop is broken instantly while when continue keyword is used, current iteration is broken and loop continues with next iteration.

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

What is the difference between double and float variables in Java?

A

In java, float takes 4 bytes in memory while Double takes 8 bytes in memory. Float is single precision floating point decimal number while Double is double precision decimal number.

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

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

A

Ans: 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
7
Q

s it compulsory for a Try Block to be followed by a Catch Block in Java for Exception handling?

A

Ans: Try block needs to be followed by either Catch block or Finally block or both. Any exception thrown from try block needs to be either caught in the catch block or else any specific tasks to be performed before code abortion are put in the Finally block.

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

When the constructor of a class is invoked?

A

Every time the “new” keyword is used

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

Can we override static methods of a class?

A

Ans: We cannot override static methods. Static methods belong to a class and not to individual objects and are resolved at the time of compilation (not at runtime).Even if we try to override static method,we will not get an complitaion error,nor the impact of overriding when running the code.

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

Is String a data type in java?

A

Ans: String is not a primitive data type in java. When a string is created in java, it’s actually an object of Java.Lang.String class that gets created. After creation of this string object, all built-in methods of String class can be used on the string object

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

When a lot of changes are required in data, which one should be a preference to be used? String or StringBuffer?

A

Ans: Since StringBuffers are dynamic in nature and we can change the values of StringBuffer objects unlike String which is immutable, it’s always a good choice to use StringBuffer when data is being changed too much. If we use String in such a case, for every data change a new String object will be created which will be an extra overhead.

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

Can a class be a super class and a sub-class at the same time? Give example.

A

Ans: If there is a hierarchy of inheritance used, a class can be a super class for another class and a sub-class for another one at the same time.

In the example below, continent class is sub-class of world class and it’s super class of country class.

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

How objects of a class are created if no constructor is defined in the class?

A

Ans: Even if no explicit constructor is defined in a java class, objects get created successfully as a default constructor is implicitly used for object creation. This constructor has no parameters.

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

What’s the benefit of using inheritance?

A

Ans: Key benefit of using inheritance is reusability of code as inheritance enables sub-classes to reuse the code of its super class. Polymorphism (Extensibility ) is another great benefit which allow new functionality to be introduced without effecting existing derived classes.

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