JAVA QUESTION PAPER 2007

SUBMITTED BY:

ER. NIKITA MAHAJAN

POLY. Vth SEM CSE

THAKUR SHIVKUMARSINGH MEMORIAL POLYTECHNIC COLLEGE,

BURHANPUR

  1. List at least five major differences between C++ and Java.

Ans:  -

                      C++

                             Java

C++ is platform-dependent.

Java is platform-independent.

C++ is mainly used for system programming.

Java is mainly used for application programming. It is widely used in window, web-based, enterprise and mobile applications.

C++ supports goto statement.

Java doesn't support goto statement.

C++ supports multiple inheritance.

Java doesn't support multiple inheritance through class. It can be achieved by interfaces in java.

C++ supports operator overloading.

Java doesn't support operator overloading.

C++ supports pointers. You can write pointer program in C++.

Java supports pointer internally. But you can't write the pointer program in java. It means java has restricted pointer support in java.

C++ uses compiler only.

Java uses compiler and interpreter both.

C++ supports both call by value and call by reference.

Java supports call by value only. There is no call by reference in java.

C++ supports structures and unions.

Java doesn't support structures and unions.

Ans:  -

  1. Describe different forms of inheritance in Java with examples.

Ans: - Inheritance is a mechanism wherein a new class is derived from an existing class. In Java, classes may inherit or acquire the properties and methods of other classes. A class derived from another class is called a subclass, whereas the class from which a subclass is derived is called a superclass.

1) Single Inheritance

Single inheritance is damn easy to understand. When a class extends another one class only then we call it a single inheritance. The below flow diagram shows that class B extends only one class which is A. Here A is a parent class of B and B would be a child class of A.

Single Inheritance

class A

{

   public void methodA ()

   {

     System.out.println ("Base class method");

   }

}

class B extends A

{

   public void methodB ()

   {

System.out.println ("Child class method");

   }

   public static void main(String args[])

   {

     B obj = new B ();

     obj.methodA(); //calling super class method

     obj.methodB(); //calling local method

  }

}

  1. Multilevel Inheritance

Multilevel inheritance refers to a mechanism in OO technology where one can inherit from a derived class, thereby making this derived class the base class for the new class. As you can see in below flow diagram C is subclass or child class of B and B is a child class of A. For more details and example refer – Multilevel inheritance in Java.

Multilevel-Inheritance

Multilevel Inheritance example program in Java

class X

{

public void methodX ()

{

System.out.println ("Class X method");

}

}

class Y extends X

{

public void methodY ()

{

System.out.println ("class Y method");

}

}

class Z extends Y

{

public void methodZ ()

{

System.out.println ("class Z method");

}

public static void main(String args[])

{

     Z obj = new Z ();

     obj.methodX();//calling grandparent class method

     obj.methodY();//calling parent class method

     obj.methodZ();//calling local method

}

}

  1. Hierarchical Inheritance

In such kind of inheritance one class is inherited by many sub classes. In below example class B,C and D inherits the same class A. A is parent class (or base class) of B, C & D. Read More at – Hierarchical Inheritance in java with example program.

https://beginnersbook.com/wp-content/uploads/2013/05/Hierarchical-Inheritance.png

  1. Hybrid Inheritance

In simple terms you can say that Hybrid inheritance is a combination of Single and Multiple inheritances.  A hybrid inheritance can be achieved in the java in a same way as multiple inheritances can be!! Using interfaces. Yes you heard it right. By using interfaces you can have multiple as well as hybrid inheritance in Java.

https://beginnersbook.com/wp-content/uploads/2013/05/Hybrid-inheritance.png


  1. How does string class differ from string buffer class?

Ans: -

Sr. No.

STRING

STRING BUFFER

1

String class is immutable.

String Buffer class is mutable.

2

String is slow and consume more memory when you concate too many string because every time its create new instance

String buffer  is fast and consume less memory when you concate string

3

String class overrides the equals () method of Object class. So you can compare the contents of two strings by equals () method.

String Buffer class doesn't override the equals() method of Object class.

  1. What is vector? How it is different from array?

Ans: -Vector implements a dynamic array. It is similar to ArrayList, but with two differences −

Vector proves to be very useful if you don't know the size of the array in advance or you just need one that can change sizes over the lifetime of a program.

Following is the list of constructors provided by the vector class.

Sr.No.

ARRAY

VECTOR

1

ArrayList is not synchronized.

Vector is synchronized.

2

ArrayList increments 50% of current array size if number of element exceeds from its capacity.

Vector increments 100% means doubles the array size if total number of element exceeds than its capacity.

3

ArrayList is not a legacy class, it is introduced in JDK 1.2.

Vector is a legacy class.

4

ArrayList is fastbecause it is non-synchronized.

Vector is slow because it is synchronized i.e. in multithreading environment; it will hold the other threads in runnable or non-runnable state until current thread releases the lock of object.

5

ArrayList uses Iterator interface to traverse the elements.

ArrayList uses Iterator interface to traverse the elements.

  1. Write the program in java and create thread of counter.

Ans: -

  1. How do we set priorities for thread?

Ans: -Thread Priorities

In the Java programming language, every thread has a priority. By default, a thread inherits the priority of its parent thread. You can increase or decrease the priority of any thread with the setPriority method. You can set the priority to any value between MIN_PRIORITY (defined as 1 in the Thread class) and MAX_PRIORITY (defined as 10).NORM_PRIORITY is defined as 5.

Each thread have a priority. Priorities are represented by a number between 1 and 10. In most cases, thread schedular schedules the threads according to their priority (known as preemptive scheduling). But it is not guaranteed because it depends on JVM specification that which scheduling it chooses.

3 constants defiend in Thread class:

  1. public static int MIN_PRIORITY
  2. public static int NORM_PRIORITY
  3. public static int MAX_PRIORITY

Default priority of a thread is 5 (NORM_PRIORITY). The value of MIN_PRIORITY is 1 and the value of MAX_PRIORITY is 10.

Example of priority of a Thread: 

class TestMultiPriority1 extends Thread{  

 public void run(){  

   System.out.println ("running thread name is:"+Thread.currentThread ().getName ());  

   System.out.println ("running thread priority is:"+Thread.currentThread ().getPriority ());  

  

  }  

public static void main(String args[]){  

  TestMultiPriority1 m1= new TestMultiPriority1 ();  

  TestMultiPriority1 m2= new TestMultiPriority1 ();  

  m1.setPriority (Thread.MIN_PRIORITY);  

  m2.setPriority (Thread.MAX_PRIORITY);  

  m1.start ();  

  m2.start ();  

   

 }  

}     

  1. Explain life cycle of thread.

Ans: -Life cycle of a Thread (Thread States)

  1. New
  2. Runnable
  3. Running
  4. Non-Runnable (Blocked)
  5. Terminated

A thread can be in one of the five states. According to sun, there is only 4 states in thread life cycle in java new, runnable, non-runnable and terminated. There is no running state.

But for better understanding the threads, we are explaining it in the 5 states.

The life cycle of the thread in java is controlled by JVM. The java thread states are as follows:

  1. New
  2. Runnable
  3. Non-Runnable (Blocked)
  4. Terminated

thread life cycle in java

1) New

The thread is in new state if you create an instance of Thread class but before the invocation of start() method.

2) Runnable

The thread is in runnable state after invocation of start() method, but the thread scheduler has not selected it to be the running thread.

3) Non-Runnable (Blocked)

This is the state when the thread is still alive, but is currently not eligible to run.

4) Terminated

A thread is in terminated or dead state when its run() method exits.

  1. Describe JDBC drivers in details.

Ans: - DBC drivers implement the defined interfaces in the JDBC API, for interacting with your database server.

For example, using JDBC drivers enable you to open database connections and to interact with it by sending SQL or database commands then receiving results with Java.

The Java.sql package that ships with JDK contains various classes with their behaviours defined and their actual implementaions are done in third-party drivers. A third party vendor implements the java.sql.Driver interface in their database driver.

JDBC Drivers Types

JDBC driver implementations vary because of the wide variety of operating systems and hardware platforms in which Java operates. Sun has divided the implementation types into four categories, Types 1, 2, 3, and 4, which is explained below −

Type 1: JDBC-ODBC Bridge Driver

In a Type 1 driver, a JDBC bridge is used to access ODBC drivers installed on each client machine. Using ODBCrequires configuring on your system a Data Source Name (DSN) that represents the target database.

When Java first came out, this was a useful driver because most databases only supported ODBC access but now this type of driver is recommended only for experimental use or when no other alternative is available.

The JDBC-ODBC Bridge that comes with JDK 1.2 is a good example of this kind of driver.

Type 2: JDBC-Native API

In a Type 2 driver, JDBC API calls are converted into native C/C++ API calls, which are unique to the database. These drivers are typically provided by the database vendors and used in the same manner as the JDBC-ODBC Bridge. The vendor-specific driver must be installed on each client machine.

If we change the Database, we have to change the native API, as it is specific to a database and they are mostly obsolete now, but you may realize some speed increase with a Type 2 driver, because it eliminates ODBC's overhead.

The Oracle Call Interface (OCI) driver is an example of a Type 2 driver.

Type 3: JDBC-Net pure Java

In a Type 3 driver, a three-tier approach is used to access databases. The JDBC clients use standard network sockets to communicate with a middleware application server. The socket information is then translated by the middleware application server into the call format required by the DBMS, and forwarded to the database server.

This kind of driver is extremely flexible, since it requires no code installed on the client and a single driver can actually provide access to multiple databases.

You can think of the application server as a JDBC "proxy," meaning that it makes calls for the client application. As a result, you need some knowledge of the application server's configuration in order to effectively use this driver type.

Your application server might use a Type 1, 2, or 4 driver to communicate with the database, understanding the nuances will prove helpful.

Type 4: 100% Pure Java

In a Type 4 driver, a pure Java-based driver communicates directly with the vendor's database through socket connection. This is the highest performance driver available for the database and is usually provided by the vendor itself.

This kind of driver is extremely flexible, you don't need to install special software on the client or server. Further, these drivers can be downloaded dynamically.

MySQL's Connector/J driver is a Type 4 driver. Because of the proprietary nature of their network protocols, database vendors usually supply type 4 drivers.

  1. Write servlet to display your name.

Ans: -