Tuesday, April 19, 2011

Core Java Interview Questions - Part 2

In the Previous part on Core Java Interview Questions, I had posted 50 questions. Since the post was getting too big, I left out some questions which are posted in this one.

Apart from the questions below, there are a few articles that I have put up (as part of the SCJP Certification series) that cover these topics, which you might find useful. You can use them to revise/review your understanding of these topics.

They are:

Static Fields & Methods
Loops & Iterators
Using Dates & Calendars
If & Switch Statements
File Navigation & I/O
Polymorphism - Method Overloading and Method Overriding
Encapsulation
Inheritance
Arithmetic Operators
Logical Operators

Questions:

51. Are true and false keywords?

The values true and false are not keywords.

52. What is a void return type?

A void return type indicates that a method does not return a value after its execution.

53. What is the difference between the File and RandomAccessFile classes?

The File class encapsulates the files and directories of the local file system. The RandomAccessFile class provides the methods needed to directly access data contained in any part of a file.

54. Which package is always imported by default?

The java.lang package is always imported by default in all Java Classes.

55. What restrictions are placed on method overriding?

Overridden methods must have the same name, argument list, and return type. The overriding method may not limit the access of the method it overrides but it can expand it. The overriding method may not throw any exceptions that are not thrown by the overridden method.

56. Which arithmetic operations can result in the throwing of an ArithmeticException?

Integer / and % can result in the throwing of an ArithmeticException.

57. What is the ResourceBundle class?

The ResourceBundle class is used to store locale-specific resources that can be loaded by a program to tailor the program's appearance to the particular locale in which it is being run.

58. What is numeric promotion?

Numeric promotion is the conversion of a smaller numeric type to a larger numeric type, so that integer and floating-point operations may take place. In numerical promotion, byte, char, and short values are converted to int values. The int values are also converted to long values, if necessary. The long and float values are converted to double values, as required.

59. To what value is a variable of the boolean type automatically initialized?

The default value of the boolean type is false.

60. What is the difference between the prefix and postfix forms of the ++ operator?

The prefix form performs the increment operation and returns the value of the increment operation. The postfix form returns the current value to the expression and then performs the increment operation on that value.

61. What is the purpose of a statement block?

A statement block is used to organize a sequence of statements as a single statement group.

62. What is the difference between an if statement and a switch statement?

The if statement is used to select among two alternatives. It uses a boolean expression to decide which alternative should be executed. The switch statement is used to select among multiple alternatives. It uses an int expression to determine which alternative should be executed.

A Switch statement with 5 Case blocks can be compared to an if statement with 5 else-if blocks.

63. What do you mean by object oreiented programming

In object oreinted programming the emphasis is more on data than on the procedure and the program is divided into objects. Some concepts in OO Programming are:

* The data fields are hidden and they cant be accessed by external functions.
* The design approach is bottom up.
* The Methods operate on data that is tied together in data structure

64. What are 4 pillars of object oreinted programming

1. Abstraction - It means hiding the details and only exposing the essentioal parts

2. Polymorphism - Polymorphism means having many forms. In java you can see polymorphism when you have multiple methods with the same name

3. Inheritance - Inheritance means the child class inherits the non private properties of the parent class

4. Encapsulation - It means data hiding. In java with encapsulate the data by making it private and even we want some other class to work on that data then the setter and getter methods are provided

65. Difference between procedural and object oreinted language

In procedural programming the instructions are executed one after another and the data is exposed to the whole program
In Object Oriented programming the unit of program is an object which is nothing but combination of data and code and the data is not exposed outside the object

66. What is the difference between parameters and arguments

While defining method, variables passed in the method are called parameters. While using those methods, values passed to those variables are called arguments.

67. What is reflection in java

Reflection allows Java code to discover information about the fields, methods and constructors of loaded classes and to dynamically invoke them. The Java Reflection API covers the Reflection features.

68. What is a cloneable interface and how many methods does it contain

The cloneable interface is used to identify objects that can be cloned using the Object.clone() method. IT is a Tagged or a Marker Interface and hence it does not have any methods.

69. What is the difference between Java Bean and Java Class

Basically a Bean is a java class but it has getter and setter method and it does not have any logic in it, it is used for holding data.

On the other hand the Java class can have what a java bean has and also has some logic inside it

70. What are null or Marker interfaces in Java

The null interfaces or marker interfaces or Tagged Interfaces, do not have method declarations in them. They are empty interfaces, this is to convey the compiler that they have to be treated differently

71. Does java Support multiple inheritance

Java does not support multiple inheritance directly like C++, because then it is prone to ambiguity, example if a class extends 2 other classes and these 2 parent classes have same method names then there is ambiguity. Hence in Partial Java Multiple inheritance is supported using Interfaces

72. What are virtual function

In OOP when a derived class inherits from a base class, an object of the derived class may be referred to (or cast) as either being the base class type or the derived class type. If there are base class functions overridden by the derived class, a problem then arises when a derived object has been cast as the base class type. When a derived object is referred to as being of the base's type, the desired function call behavior is ambiguous.

The distinction between virtual and not virtual is provided to solve this issue. If the function in question is designated "virtual" then the derived class's function would be called (if it exists). If it is not virtual, the base class's function would be called.

73. Does java support virtual functions

No java does not support virtual functions direclty like in C++, but it supports using Abstract class and interfaces

74. What is JVM

When we install a java package. It contains 2 things
* The Java Runtime Environment (JRE)
* The Java Development Kit (JDK)

The JRE provides runtime support for Java applications. The JDK provides the Java compiler and other development tools. The JDK includes the JRE.

Both the JRE and the JDK include a Java Virtual Machine (JVM). This is the application that executes a Java program. A Java program requires a JVM to run on a particular platform

75. What is the difference between Authentication and Authorization

Authentication is a process for verifying that an individual is who they say they are. Authorization is an additional level of security, and it means that a particular user (usually authenticated), may have access to a particular resource say record, file, directory or script.

76. What types of values does boolean variables take

It only takes values true and false.

77. Which primitive datatypes are signed.

All primitive datatypes are signed except char and Boolean

78. Is char type signed or unsigned

char type is integral but unsigned. It range is 0 to 2^7-1

79. What forms an integral literal can be

decimal, octal and hexadecimal, hence example it can be 28, 034 and 0x1c respectively

80. Why is the main method static

So that it can be invoked without creating an instance of that class

81. What is the difference between class variable, member variable and automatic(local) variable

class variable is a static variable and does not belong to instance of class but rather shared across all the instances of the Class.

member variable belongs to a particular instance of class and can be called from any method of the class

automatic or local variable is created on entry to a method and is alive only when the method is executed

82. When are static and non static variables of the class initialized

The static variables are initialized when the class is loaded

Non static variables are initialized just before the constructor is called

83. How is an argument passed in java, is it by copy or by reference?

If the variable is primitive datatype then it is passed by copy.
If the variable is an object then it is passed by reference

84. How does bitwise (~) operator work

It converts all the 1 bits in a binary value to 0s and all the 0 bits to 1s, e.g 11110000 gets coverted to 00001111

85. Can shift operators be applied to float types.

No, shift operators can be applied only to integer or long types (whole numbers)

86. What happens to the bits that fall off after shifting

They are discarded (ignored)

87. What are the rules for overriding

The rules for Overriding are:

Private method can be overridden by private, protected or public methods
Friendly method can be overridden by protected or public methods
Protected method can be overridden by protected or public methods
Public method can be overridden by public method

88. Explain the final Modifier

Final can be applied to classes, methods and variables and the features cannot be changed. Final class cannot be subclassed, methods cannot be overridden

89. Can you change the reference of the final object

No the reference cannot be changed, but the data in that object can be changed

90. Can abstract modifier be applied to a variable

No it can be applied only to class and methods

91. Where can static modifiers be used

They can be applied to variables, methods and even a block of code, static methods and variables are not associated with any instance of class

92. When are the static variables loaded into the memory

During the class load time

93. When are the non static variables loaded into the memory

They are loaded just before the constructor is called

94. How can you reference static variables

You can refer to static variables directly using the class name and you dont need any object instance for it.

Ex: ClassTest.execMethod(); can be used to access the execMethod() method of the class ClassTest

95. Can static method use non static features of there class

No they are not allowed to use non static features of the class, they can only call static methods and can use static data

96. What is static initializer code

A class can have a block of initializer code that is simply surrounded by curly braces and labeled as static e.g.

public class Test{
static int =10;
static{
System.out.println("Hiiiiiii");
}
}

And this code is executed exactly once at the time of class load

97. Where is native modifier used?

It can refer only to methods and it indicates that the body of the method is to be found else where and it is usually written in language other than Java.

98. When do you use continue and when do you use break statements

When continue statement is applied it prematurely completes the iteration of a loop.
When break statement is applied it causes the entire loop to be abandoned.

99. What do you understand by late binding or virtual method Invocation.

When a compiler for a non object oriented language comes across a method invocation, it determines exactly what target code should be called and build machine language to represent that call. In an object oriented language, this is not possible since the proper code to invoke is determined based upon the class if the object being used to make the call, not the type of the variable. Instead code is generated that will allow the decision to be made at run time. This delayed decision making is called as late binding

100. Can Overridden methods have different return types

No they cannot have different return types

101. If the method to be overridden has access type protected, can subclass have the access type as private

No, it must have access type as protected or public, since an overriding method must not be less accessible than the method it overrides


102. What are the Final fields & Final Methods ?

Fields and methods can also be declared final. A final method cannot be overridden in a subclass. A final field is like a constant: once it has been given a value, it cannot be assigned again.

If you have any questions that you want answer for - please leave a comment on this page and I will answer them.

If you have any more questions on Other Java Topics that you have faced during your interviews and wish to add them to this collection - pls drop a note to anandvijayakumar007@gmail.com and I shall be glad to add them to this list.

Previous Set of Core Java Interview Questions - Part 1

Other Java & J2EE Interview Questions

4 comments:

  1. Can I Override the Static method of a parent class in my child class?

    ReplyDelete
    Replies
    1. I think you can - as long as the new/overriding method too is static.

      However, you cannot have an instance method (non-static) in the child class with the same name as the static method in the parent or vice versa.

      Delete
  2. 83. How is an argument passed in java, is it by copy or by reference?

    If the variable is primitive datatype then it is passed by copy.
    If the variable is an object then it is passed by reference

    ??
    all pass by value?

    ReplyDelete
    Replies
    1. Yes, the term pass by copy refers to pass by value only.

      Delete

© 2013 by www.inheritingjava.blogspot.com. All rights reserved. No part of this blog or its contents may be reproduced or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, or otherwise, without prior written permission of the Author.

ShareThis

Google+ Followers

Followers