Thursday, October 6, 2016

 Java Programming 


                                                  

                               Interview Question & Answer
1.    List out Java Keywords.
Ans:
Class
continue
For
new
switch
case
do
if
private
This
break
double
long
catch
catch
extends
int
short
try

1.    Write about one of the SELECTION statements of Java.
Ans:
Java language supports the following selection statements of java
(1) if statement (2)if-else (3)nested if..else (4)else if ladder
1.   If Statement:
This is a control statement to execute a single statement or a block of code, when the given condition is true and if it is false then it skips if block and rest code of program is executed.
Syntax:
if(conditional)
{
statements;
}
Example:
int n = 10;
if(n%2 = = 0)
{
System.out.println("This is even number");
}

2.    Write the usage of break and continue statements
Ans:
Break :In java the break statement has 3 uses
   1.It terminates a statement sequence in  a switch statement.
   2.it  can be used to exit a loop.
   3.it can be used as a civilized form of go to statement.
                       Syntax :
                                       break;
2.continue: In while and Do while loops , a continue statements causes the control to be transferred directly to conditional expression and then to continue the iteration process.
              .In the case of loop,the increment section of the loop is executed before the test condition is evaluated.
              .The general form of Continue is
                        Continue;

3.    List out the properties of static variables and methods.
Ans: static variables and methods are declared with keyword static.
Ex: static int a;
      Static void swap(int x, int y);
·         Static variables are initialized to value zero.
·         Java creates only one copy for a static variable irrespective of number of objects of a class.
Properties of a static methods:
·         They can call other static methods
·         They must only access static data
·         They can not refer to ‘this’ or ‘super’ keywords in any way.

4.    write about NEW operator
ans:
new operator is used to create the object dynamically.
Syntax: class_name obj=new class_name();
Advantages of new operator:
1.    no memory wastage
2.    no insufficient memory message unless total memory is used

5.    Define a class in java.
Ans: it is a collection of instance variables(fields) and instance methods of logically related.
A class acts as a data type and is used to create objects of that data type.
Syntax:
Class class_name
{
  Data type instance variables;
  Data type instance variables;
  Datatype method_name(parameter list)
 {
   Method body;
 }
 ……….
 ……….
}

6.    Write about Class Path variable.

Ans: It is an environment variable to setting the PATH environment variable in the convenient way to run the java programs from any directory without mention the full path of the command prompt.
Steps to setting the class path in Windows o/s
1.    place the my computer->right click->go to properties->click on advanced system settings->select advanced tab->click on environment variable->in the system variables-> select path->click on edit and type the path. Ex: c:\programfiles\java\jdk1.5.0-05\bin

7.    List out Thread properties
Ans: the thread class defines several priority constants:
        MIN_PRIORITY=1
        NORM_PRIORITY=5
        MAX_PRIORITY=10

8.    List out some of the source of errors in programming.  
Ans:
Errors are classified into two types
1.    compilation time errors
2.    Run time errors
compilation time errors:
1.    Missing semicolons
2.    Miss match brackets in classes and methods
3.    Misspelling of identifiers and keywords
4.    Undeclared variables
Run time Errors
1.    dividing on integer by zero
2.    out of the bounds of an array
3.    trying to store a value into an array of incompative class or type

10. Define an applet?
Ans:
Applet:
·                     Applet is a small java program for internet application.
·                     It is located in different computers and Applet can be downloaded through internet and executed on a local computer using java enabled browser.
·                     Applet is used to create animations and complex games.
It represents in HTML page.

11.What is difference between Path and Classpath?
Path and Classpath are operating system level environment variales. 
Path is used define where the system can find the executables(.exe) files and classpath is used to specify the location .
class files.

12.What are local variables?
Local variables are those which are declared within a block of code like methods. 
Local variables should be initialized before accessing them.

13.What are instance variables?
Instance variables are those which are defined at the class level. Instance variables need not be initialized before using them as they are automatically initialized to their default values.

14.How to define a constant variable in Java?
The variable should be declared as static and final. So only one copy of the variable exists for all instances of the class
and the value can't be changed also.static final int MAX_LENGTH = 50; is an example for constant.

15.Should a main() method be compulsorily declared in all java classes?
No not required. main() method should be defined only if the source class is a java application.

16.What is this in java?

It is a keyword that that refers to the current object.

17.What is Inheritance?
Inheritance is a mechanism in which one object acquires all the properties and behavior of another  object of another class.It represents IS-A relationship. It is used for Code Re-usability and Method Overriding.

18.Which class is the super class for every class.

Object class.

19.Why multiple inheritance is not supported in java?

To reduce the complexity and simplify the language, multiple inheritance is not supported in java in case of class.

20.What is difference between aggregation and composition?
Aggregation represents weak relationship whereas composition represents strong relationship. 
For example: bike has an indicator (aggregation) but bike has an engine(composition).

Q21.How can we pass argument to a function by reference instead of pass by value?

Ans:  In java, we can pass argument to a function only by value and not by reference.

Q22.How an object is serialized in java?

Ans: In java, to convert an object into byte stream by serialization, an interface with the name Serializable is implemented by the class. All objects of a class implementing serializable interface get serialized and their state is saved in byte stream.

Q23. When we should use serialization?

Ans: Serialization is used when data needs to be transmitted over the network. Using serialization, object’s state is saved and converted into byte stream .The  byte stream is transferred over the network and the object is re-created at destination.

Q24. When the constructor of a class is invoked?

Ans: The constructor of a class is invoked every time an object is created with new keyword.

For example, in the following class two objects are created using new keyword and hence, constructor is invoked two times.


public class const_example {

const_example() {

System.out.println("Inside constructor");

}

public static void main(String args[]) {

const_example c1=new const_example();

const_example c2=new const_example();

}

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class const_example {

const_example() {

System.out.println("Inside constructor");

}

public static void main(String args[]) {

const_example c1=new const_example();

const_example c2=new const_example();

Q25. Can a class have multiple constructors?

Ans: Yes, a class can have multiple constructors with different parameters. Which constructor gets used for object creation depends on the arguments passed while creating the objects.

Q26. Can a main() method be overloaded?
Yes. You can have any number of main() methods with different method signature and implementation in the class.

Q27. Can a main() method be declared final?
Yes. Any inheriting class will not be able to have it's own default main() method.

Q28. Does the order of public and static declaration matter in main() method?
No. It doesn't matter but void should always come before main().

Q.29.Can a source file contain more than one class declaration?
Yes a single source file can contain any number of Class declarations but only one of the class can be declared as public.

Q.30.What is a package?
Package is a collection of related classes and interfaces. package declaration should be first statement in a java class.




1 comment:

  1. Nice blog, very useful information for freshers. Thank you for sharing.
    java classes in pune

    ReplyDelete