most common core java practical interview questions - part 1

12
10 Most Common Core Java Practical Interview Questions-Part 1 By Chandani Thacker [email protected] www.attuneuniversity.com

Upload: attune-world-wide

Post on 15-Aug-2015

73 views

Category:

Technology


3 download

TRANSCRIPT

10 Most

Common Core

Java Practical

Interview

Questions-Part 1

By Chandani Thacker

[email protected] www.attuneuniversity.com

10 Most Common Core Java Practical Interview Questions-Part 1 1

Abstract Class can have constructor.

Right or Wrong?

First lets make a simple program. With out abstract class :

Here is output of above program :

Inside Class A

Inside Class A

Inside Class B

2 10 Most Common Core Java Practical Interview Questions-Part 1

Modification 1: Abstract Class can have constructor. Right

or Wrong?

Now let’s make some changes in previous program. Make class A abstract :

This gives compile time error. Cannot instantiate the type A. As you have

created class A as abstract.

10 Most Common Core Java Practical Interview Questions-Part 1 3

Modification 2 Abstract class can have constructor. Right or

Wrong?

Now let’s do again few changes. Remove the line that created object of

class A.And run the program.

Output is as follows:

Inside Class A

Inside Class B Here, class A is abstract. But it has constructor. That is by default called when class B constructor is called. As B is extending A.

4 10 Most Common Core Java Practical Interview Questions-Part 1

Modification 3 : Abstract class can have constructor. Right

or Wrong?

As shown in above example, abstract class can have parameterizes

constructor also.

Output is as follows:

Inside Class A : value is : 10

Inside Class B

10 Most Common Core Java Practical Interview Questions-Part 1 5

Divide by 0 Exception or Infinity!?

As Expected, above program will give exception when you run it. Output is as follows. Exception in thread "main" java.lang.ArithmeticException: / by zero at org.attune.interviewPrograms.DevideByZero.main(DevideByZero.java:5)

Modification 1: Divide by 0 Exception or Infinity!?

Here modification at line 5. Instead of 0 we have written 0.0(double). Now Output is not an exception. Output is Infinity.

6 10 Most Common Core Java Practical Interview Questions-Part 1

Difference between String and String

Buffer

Consider the following program:

Output is:

S1 is: 1234 s2 is 123 s1==s2 is: false

A is: 1234 B is 1234 A==B is: true

Here s1 is simple string. When we add character 4 to string, it will not

affect s2.Even though s1 is assigned in to s2.

But a is string buffer. When we add character 4 to a, b is also changed. As a

is assigned to b.

10 Most Common Core Java Practical Interview Questions-Part 1 7

Use of Static with Example.

Output is as follows :

Inside Static block .a is : 12 Inside Static method Inside Static block 2 Inside Main .a is : 13 As per example, a is initialized first, then static block is executed without calling it, once again other static block will be executed. That will call the callMe method. In last main method is executed. Here you can see to use variable a, or to call method callMe you don’t require object .

8 10 Most Common Core Java Practical Interview Questions-Part 1

Modification 1: Use of Static with Example.

Here we have added a new variable b. That is not static. So accessing it from static block or accessing it from main method will give compile time error. Main is also static method. You cannot use non static variable inside.

10 Most Common Core Java Practical Interview Questions-Part 1 9

Scope of a variable

Consider a simple program.

Variable a is declared and initialized with 10.So when we print a, out put is

10.

10 10 Most Common Core Java Practical Interview Questions-Part 1

Modification 1 : Scope of a variable

Now let’s do some modification in above program.

This program will give compile time error. As a is declared inside method

again but not initialized again. So before printing we must initialize it.

Variable declared outside is not now known because we have declared

variable with same name inside.

10 Most Common Core Java Practical Interview Questions-Part 1 11

Modification 2 : Scope of a variable

Now initialize a variable inside method. So compile time error will be

removed.

When you run this program, 20 is output.

Value 10 is initialized outside, that will be lost. Value initialized inside

method will remain there.