Java Programming 2017-18 Back Examination Previous Year Paper Solution
Long Questions

Q2 a) Describe the structure of java program. [Marks: 5]

Ans:

b) Explain the features of java. [Marks: 5]

Ans: Features of Java programming language:

  1. High level and general purpose: Java is a high-level and general-purpose language as it enables a programmer to write programs that are independent of a particular type of computer and are close to human languages and further from machine languages.
  2. Object-Oriented: Everything in Java is an object. Object-oriented means we organize our software as a combination of different types of objects that consists of both data and behavior. So, Java is called an object-oriented programming language. 
  3. Platform independent: Java compiler produces a unique type of code called bytecode, unlike c,c++ compiler where the compiler produces only natively executable code for a particular machine. When the Java program runs in a particular machine it is sent to a java compiler, which converts this code into an intermediate code called bytecode. This bytecode is sent to Java virtual machine (JVM) which resides in the RAM of any operating system. JVM recognizes the platform it is on and converts the bytecodes into native machine code. Hence java is called platform-independent language.
  4. Distributed: Java is distributed because it facilitates users to create distributed applications in Java. RMI and EJB are used for creating distributed applications. 
  5. Dynamic: Java is a dynamic language. It supports dynamic loading of classes. It means classes are loaded on demand.

Q3. a) Can java directly support multiple inheritance? Illustrate your answer with an example java program. [Marks: 5]

Ans: When one class extends more than one classes then this is called multiple inheritance. Java doesn’t support multiple inheritance directly to avoid the ambiguity caused by it.

Why Java does not support multiple inheritance?

One example of the problem that occurs in multiple inheritance is the diamond problem.

Diamond-problem-Multiple-Inheritance-Issue

Here, we have two classes B and C inheriting from A. Assume that B and C are overriding an inherited method and they have their own implementation. Now D inherits from both B and C doing multiple inheritance. Now, if D wants to use the same method which overridden method would be called? Will it be from B or C? Here we have an ambiguity because of multiple inheritance. That’s the main reason why Java doesn’t support multiple inheritance.

b) Write a program that collects the input as a decimal number of integer type and convert it into a string of hexadecimal number. [Marks: 5]

Ans: Java Program code:

/* Java program to convert a decimal 
 number to hexadecimal number */ 
import java.io.*; 
  
class JavaExamples 
{ 
    // function to convert decimal to hexadecimal 
    static void DecimalToHexadecimal(int n) 
    {    
        // char array to store hexadecimal number 
        char[] HexaDeciNumber = new char[100]; 
       
        // counter for hexadecimal number array 
        int i = 0; 
        while(n!=0) 
        {    
            // temporary variable to store remainder 
            int temp  = 0; 
           
            // storing remainder in temp variable. 
            temp = n % 16; 
           
            // check if temp < 10 
            if(temp < 10) 
            { 
                HexaDeciNumber [i] = (char)(temp + 48); 
                i++; 
            } 
            else
            { 
                HexaDeciNumber [i] = (char)(temp + 55); 
                i++; 
            } 
           
            n = n/16; 
        } 
       
        // printing array in reverse order 
        for(int j=i-1; j>=0; j--) 
            System.out.print(HexaDeciNumber [j]); 
    } 
      
    // Main Method 
    public static void main (String[] args)  
    { 
        int n = 4200; 
        DecimalToHexadecimal(n); 
    } 
} 

Q4. a) What is the objective of passing parameters in java? Explain various ways of passing parameters with suitable example in java. [Marks: 5]

Ans: To allow a method to modify a argument, you must pass in an object. There are different ways in which parameter data can be passed into and out of methods and functions.

Types of parameters:

There are two types of parameters:

  1. Formal Parameter
  2. Actual Parameter

Lets understand the difference between two types of parameter using an example:

Consider a function function_B(int a, int b) is called from another function function_A(). In this case, function_A() is called the “caller function” and function_B() is called the called function“.

Also, the arguments which function_A() sends to function_B(int a, int b) a while calling are called actual arguments, and the parameters of B are called formal arguments.

Code Example:

import java.io.*; 
 
class GFG  
{ 
   
    static void decToHexa(int formal_arg) 
    {    
        // Body
    } 
 
    // Main Method 
    public static void main (String[] args)  
    { 
        int actual_arg = 2545; 
        decToHexa(actual_arg); 
    } 
} 

Here, main() function is the “caller function” and decToHexa(int n) is called the called function“. and actual_arg is the actual argument and formal_arg is the formal argument.

There are two way using which a parameter can be passed:

  1. Pass By Value
  2. Call by reference

Pass By Value:

In Pass By Value method, the copy of actual parameter values is copied to formal parameters, and these formal parameters are used in the called function. The changes made on the formal parameters inside the function do not affect the values of actual parameters. This means, after the execution control comes back to the calling function, the actual parameter values remain the same.

Example Code:

class CallByValue { 
  
    // Function to change the value 
    // of the parameters 
    public static int doubleSumFunc(int a, int b) 
    { 
        a = a + a;
        b = b + b;
        int c=a+b;
        return c; 
    } 
}

class ExampleCode{ 
    public static void main(String[] args) 
    { 
        int num1 =10;
        int num2 = 20;
        // Instance of class is created 
        CallByValue obj = new CallByValue(); 
        int sum = obj.doubleSumFunc(num1 , num2 );
        int localsum = num1 + num2;
        System.out.println("Sum of num1 and num2 is : " + localsum );
        System.out.println("Sum of both number is : " + sum);
        
    } 
} 

In the above example program, the variables num1 and num2 are called actual parameters and the variables a and b are called formal parameters. The value of num1 is copied into a and the value of num2 is copied into b. The changes made on variables a and b does not effect the values of num1 and num2.

Call by reference:

In Call by Reference method, the memory location address of the actual parameters is copied to formal parameters. The changes made on the formal parameters effects the values of actual parameters.  This method is efficient in both time and space.

Example Code:

import java.io.*;
 
class CallByReference { 
 
    int a, b;
    public CallByReference(int x, int y){
       a = x;
       b = y;
    }
    // Function to change the value of the parameters 
    public static void ChangeValue(CallByReference obj) 
    { 
        obj.a = obj.a + 10; 
        obj.b = obj.b + 20;
    } 
}
 
class ExampleCode{ 
    public static void main(String[] args) 
    { 
        int num1 =10;
        int num2 = 20;
        CallByReference object  = new CallByReference(5,10);
 
        // Changing values in function 
        object.ChangeValue(object); 
        System.out.println("Value of a: " + object.a  + " & b: " + object.b); 
 
    } 
} 

b) Explain various methods involved in the life cycle of an applet. [Marks: 5]

Ans: Life Cycle Of An Applet:

There are different stages involved in the life cycle of an applet:

applet-life-cycle

the life cycle of an applet starts with init() method and ends with destroy() method.

init(): The init() method is the first method to execute when the applet is executed. Variable declaration and initialization operations are performed in this method.

start(): The start() method contains the actual code of the applet that should run. The start() method executes immediately after the init() method. It also executes whenever the applet is restored, maximized or moving from one tab to another tab in the browser.

stop(): The stop() method stops the execution of the applet. The stop() method executes when the applet is minimized or when moving from one tab to another in the browser.

destroy(): The destroy() method executes when the applet window is closed or when the tab containing the webpage is closed. stop() method executes just before when destroy() method is invoked. The destroy() method removes the applet object from memory.

paint(): The paint() method is used to redraw the output on the applet display area. The paint() method executes after the execution of start() method and whenever the applet or browser is resized.

Q5. a)
Ans:

b) With relevant examples discuss exception handling in java. [Marks: 5]

Ans: Exception handling allows us to handle the runtime errors caused by exceptions. Java provides inbuilt exceptional handling. We use a try-catch block for exception handling in java. Try is the start of the block and catch is at the end of the try block to handle the exceptions.

The general syntax of exception handling is:

class Program{
   try{
      // Code where there is chance of raising exception
   }
   catch(){
     // Exception handling code on raising exception
   }
}

Lets consider a simple exception “Divide by 0”.

Java Code:

class Program{
 public static void main(String args[]) {
  int divisor = 0;
  int number = 10;
  try {
      int fraction = number/divisor;
      System.out.println("The above line having an error!!, hence this line will not be executed");
  } catch (ArithmeticException e) {
      System.out.println("Exception Raised = " + e);
  }
      System.out.println("Exception Demo Completed");
 }
}

Q6. a) Explain in detail about AWT event hierarchy. [Marks: 5]

Ans: We can represents AWT event hierarchy using below diagram:

b) What is meant by stream? What are the types of streams and the classes? Explain in detail. [Marks: 5]

Ans: What is Stream?

A Stream represents an input source or an output destination which could be a file, i/o devise, other programs, etc.

What are the types of streams?

In general, a Stream will be an input stream(to read data from a source) or, an output stream(to write data to a destination).

Based on the data they handle there are two types of streams:

  1. Byte Streams: These handle data in bytes (8 bits)
  2. Character Streams: These handle data in 16 bit Unicode.

Q7. What is event handling in java? List out the available event classes and listener interfaces with suitable examples. [Marks: 10]

Ans: Event refers to the change in state of any object. For Example