IMPORTANT QUESTION ANSWER for ICSE ISC BSC BCA BTECH

IMPORTANT QUESTION ANSWER for ICSE ISC BSC BCA BTECH


IMPORTANT QUESTION ANSWER ICSE ISC BSC BCA BTECH


What is a variable?

Ans. It is nothing but place holder of data. It is a memory location. Example int a, b; Here a and b is are variable of datatype int.

State the difference between token and identifier, variable.

Ans. The smallest individual unit of the program is called Token. e.g:  Identifiers, Keyword, Literals, etc. whereas an identifier is a name given to different parts of a program, it is also known as variables. e.g. variable, functions, classes, etc.
Variable: A memory location that stores a value is known as a variable. It is like a box in memory where we keep or store values. E.g. int a;

Explain the Instance Variable. Give an example.

Ans. Variables associated with the instance of a class are called instance variables. E.g. 
class ABC
{
    int a, b, c;    // Instance variable
    void sum()
    {
         a = 20;
         b = 30;
         c = a + b;
         System.out.println(c);
     }
}

For example the term Object and Class using an example. 

Ans. Object: An instance of a class called Object. The table is an instance of class Furniture.
Class: Blueprint of an object is called Class. For example, mango, apple and orange are members of the class fruit.

 Name four OOP principles.

Ans. Abstraction, Encapsulation, Polymorphism, Inheritance
Abstraction: Abstraction refers to the act of representing essential features without including the background details or explanations.
Polymorphism: polymorphism refers to a programming language's ability to process objects differently depending on their data type or class. More specifically, it is the ability to redefine methods for derived classes. For example, given a base class shape, polymorphism enables the programmer to define different area methods for any number of derived classes, such as circles, rectangles, and triangles. No matter what shape an object is, applying the area method to, it will return the correct results
Encapsulation: The wrapping up of data and methods into a single unit is called Encapsulation.
.Inheritance: Inheritance is the process by which objects of one class acquire the properties of objects of another class. Different kinds of objects often have a certain amount in common with each other. Mountain bikes, road bikes, and tandem bikes, for example, all share the characteristics of motorbike. Yet each also defines additional features that make them different: tandem bicycles have two seats and two sets of handlebars; road bikes have drop handlebars; some mountain bikes have an additional chain ring, giving them a lower gear ratio.



Define Byte code and JVM.

Ans. JVM: A JVM is a machine within a machine. it is like a real Java processor, enabling Java byte code to be executed as actions or operating system calls on any processor regardless of the operating system.
Byte code is the compiled format for Java programs. Once a Java program has been converted to byte code, it can be transferred across a network and executed by Java Virtual Machine (JVM). Byte code files generally have a .class extension

What Is a Package?

A package is a namespace that organizes a set of related classes and interfaces. Conceptually we can think of packages as being similar to different folders on our computer. We might keep HTML pages in one folder, images in another, and scripts or applications in yet another. Because software written in the Java programming language can be composed of hundreds or thousands of individual classes, it makes sense to keep things organized by placing related classes and interfaces into packages.

Mention any two attributes required for class declaration.

Ans: Variables and function or state and behavior.

Define Constructor. When the constructor is called?

Ans: A constructor is the special member function that is automatically called when an instance of a class is declared.
A constructor is called when an object of the class is created.

Why do we need a constructor as a class member?

Ans: Constructors have one purpose in life: to create an instance of a class.

What is a default constructor? Why is a default constructor called so?

Ans: A default constructor is a special member function that initializes the objects.
It is called because it declares no parameters. It gets invoked without any parameter

What are the characteristics of constructor function?

Ans: Constructors are the special member functions and have the same name as class and it is called automatically when object being create.

What is parameterized constructor?
Ans: The constructors which can take different parameters are referred to as Parametrized Constructors.

What is constructor overloading?.
Ans: Defining more than one constructor for a class where they differ in the parameter types or number of parameters passed to the constructor is called overloading.

What is a method or function?
Ans. When we define a set of code within a block having a name is called method. It can call or invoke at any time.
Example:
int sum(int x, int y)
{
            int z=x+y;
            return z;
}

What is the Actual parameter?
Ans: The parameter given to the calling function is called actual parameter.  Actual parameters (also known as arguments) are what are passed by the caller.





What is a Formal parameter?

Ans: the Identifier used in a method to stand for the value that is passed into the method by a caller. Formal parameters are also known as parameters of the function.

What is the purpose of the new operator?

Ans: The new operator is used for the allocation of memory. It can be used for the creation of a new object. E.g. Classex ob=new Classex();


Explain the function of a return statement.

Ans: A return statement ends the processing of the current function and returns control to the caller of the function.

Q. What is call by value?
Ans:  In call by value arguments are passed by the value, which means that a copy of the arguments is passed to the method can make changes to the value of this copy but can not change the values of the original variables in the calling method.

State the difference between Constructor and Method.
Constructor
Method
1.Constructor name is the same as the class name
The class name should not be the same
2. It is used only for initializing data members.
Can do any type of calculation or printing.
3. It has no return type not even void
It has a return type.
4. It is called automatically when object is being created.
It has to be called or invoked.

What does the following mean?
     Employee staff = new Employee ( );
Ans: The statement means that staff is an the object of class Employee and it is being created.


State the difference between = = operator and equals ( ) method.


Ans: equals() is string function whereas == is a relational operator. The difference between them is that the equal() method compares the characters inside two string whereas == operator compares the two object references that whether the two objects belongs to the same class or not.

Java: The Complete Reference

What is the role of the keyword void in declaring functions?

Ans: when we want a function that has no return value is used void.

If a function contains several return statements, how many of them will be executed?
Ans: One

Which OOP principle implements function overloading?
Ans: Polymorphism

OCA/OCP Java SE 7 Programmer I & II Study Guide (Exams 1Z0-803 & 1Z0-804)

Define an impure function and Pure function.

Ans: Pure Function:  only returns a value without bringing about any change in the values being passed to it, is called a pure function. In other words, there is no change in the state of the function.
Example:
Static Boolean max(int m, int n)
{
            if(m == n)
                        return true;
            else
                        return false;
}

Impure Function: A function that brings about a change in the argument that it receives. Its arguments will always be reference types. It may or may not return value. In other words, an impure function brings about a change in the state of the function. This change in state is called the side effect of calling an impure function.
Example:
static void count(Number n) 
{
            n.counter=n.counter+1;
}

The above function takes the argument n as an object of class Number. The counter variable of object n is incremented by 1 every time this function is called.

Explain function overloading with an example.
Ans: Function Overloading. When more than one function has the same name but the type and number of arguments are different, they are said to be overload. Thus, the parameter list of each function should be unique. Function overloading implements the concept of polymorphism.
Example of function overloading:
Float sum(float a, float b)
{
            return a+b;
}
int sum(int a, int b, int c)
{
            return a+b+c;
}

What is meant by an infinite loop? Give an example.
An infinite loop is a loop whose test condition is always true. This type of loop never ends by itself. For example:
for(i=1;i>0;i++)
{
      System.out.println(“Hi”);
}

Explain the function of each of the following with an example:
continue:
Ans: The continue statement causes the loop to be continued with the next iteration after skipping any statements in between.
Do
{
......................
......................
if(condition)
continue;
}while(test condition);
break;
Ans: An early exit from a loop can be accomplished by using the break statement. When the break statement is encountered inside a loop, the the loop immediately exists.
Ex. for(.................................)
{
..........................
if(condition)
break;
}

State one similarity and one difference between while and do-while loop.
Ans: While and do while both are to repeat the process as long as the user wants.
The difference between while and do-while is that while check condition first whereas do while checks the condition at the end. 


State any two objectives of using Arrays.
Ans: 1. Use hold elements in a contiguous memory location.
2. Arrays are used to group storage locations.

Explain the term for loop with an example.
Ans: The process of repeatedly executing a block of statements is known as loop.

What are Nested Loops? Give one example.
Ans: Loop within a loop is called nested loop.
for(i=0;i<5;i++)
{
            for(j=0;j<I;j++)
            {
                        ……………….
                        ………………
            }
}


Java 

Become a Java programmer in 3 hours


Few more Question Answer Topic

Important interview QA

Java Interview QA

Constructor in Java

Constructor in Java

Constructors in Java

What is a constructor?

Ans: A constructor is a special member method that is automatically called when an instance of a class is declared. A constructor is called when an object of the class is created.
It is a function.
A constructor must have the same name as the class name.
It has no return type not even void.
It is called when the object of the class is created.
It is used to initialize the instance variables.
These methods can only be executed at the time of the creation of an object, it is not possible to use them later.
Remember that Java creates the constructor without arguments automatically, however, if we have added a constructor with arguments, then Java will no longer add this constructor for us and it is our responsibility to add it if we are going to need it, in fact for good practice, we should always add it that we defined a constructor with arguments. 

Why do we need a constructor as a class member?

Ans: We need a constructor as a class member to initialize the value for a class variable and to create objects.

Why does a constructor should be defined as public?


Ans: A constructor should be defined in the public section of a class so that its objects can be created in any function.

What are the types of Constructors used in a class?

Ans: The different types of constructors are as follows:
Default Constructors.
Parameterized Constructor.
Copy Constructors.

 What is a default constructor? Why a default a constructor is called so?


Ans: If a class has no constructor defined, the compiler creates a constructor called default constructor which as no arguments. It initializes the data members by a default value.

Explain the Parameterized constructor?

Ans: The constructors which can take different parameters are referred to as Parameterized Constructors.
 Example:
class  Summation
{
   int x,y,z;
   public Summation(int x,int y)
   {
            this. x=x;
            this.y=y;
   }
   int sum()
   {
            z=x+y;
   }
}

What is a copy, constructor?


A copy constructor creates a temporary object of a class. It is used to copy the initialize values of the instance variables of an object to the instance variables of another object.

Mention some characteristics of constructors.

Ans: The special characteristics of constructors are:
The name of the constructor is the same as that of class
Constructors should be declared in the public section of the class.
They have invoked automatically when an object of the class is created.
They do not have any return type not even void.
They can accept arguments.
A class can have more than one constructor.
Default constructor does not accept parameters.
Constructor can be overloaded.
If no constructor is present in the class the compiler provides a default constructor.

State the difference between Constructor and Method.

Ans:
The constructor name and the class name should be the same whereas a Method can have any name
The constructor has no return type but a method has any type such as int, double, float, etc.
Invoked as soon as the object is created whereas the method invoked after the object is created.
Can be called only once but the method can call any number of times.
Constructors cannot be abstracted, final, static or synchronized but methods can be abstracted, final, static or synchronized
It is used to initialize whereas it is used to do any operation.

How compiler and JVM can differentiate constructor and method definitions of both have the same class name?

It differentiates by using return type, if there is a return type it is considered as a method else it is considered as a constructor.

Can we call subclass constructor from superclass constructor?


One word answer NO, We cannot call subclass constructor from a superclass constructor.

What is constructor overloading?


Ans: Possessing more than one constructor with a different signature by a class is known as constructor overloading.

When do we need Constructor Overloading? Or why do we overload constructors in Java?

Ans:
Let us consider the following example
Class Summation
{
   int a, b;
  Summation ()
   {
a=10;
b=20
    }
  Summation (int m, int n)
   {
a=m;
b=n;
    }
   Summation (Summation x)
   {
a=x.a;
b=x.b;
    }
In the above program, three constructors are used with different types of parameters, which means we can initializing an object in different ways. This can be done using constructor overloading. Distinctive constructors can do different work by executing different codes and are called based on the type and no of the parameters passed. According to the situation, a constructor is called with a specific number of parameters among overloaded constructors.

Do we have destructors in Java?

No. Java has its own garbage collector inbuilt and Garbage Collector takes care of clearing up memory allocated to object.

Full Example of Constructor
In this program autono and km is initialize in the constructor.
import java.io.*;
public class Automobile
{
    int autono,km;
    double rate;
    String name;
    Automobile()   // Constructor Method
    {
        autono=0;
        km=0;
    }
    void input()throws IOException    // Input Method
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter automobile no, km and name:");
        autono=Integer.parseInt(br.readLine());
        km=Integer.parseInt(br.readLine());
        System.out.println("Enter Name:");
        name=br.readLine();
        
    }
    void calculate()                          // Calculate method
    {
        if(km<=1)
         rate=25;
        else if(km>1 && km<=6)
         rate=25+10;
        else if(km>6 && km<=12)
         rate=25+10+15;
        else if(km>12 && km<=18)
         rate=25+10+15+20;
        else
         rate=25+10+15+20+25;
        }
        void display()
        {
            System.out.println("Automobile no=" + autono);
            System.out.println("Name=" + name);
            System.out.println("Rate=" + rate);
        }
        public static void main(String args[])throws IOException
        {
            Automobile ob=new Automobile();
            ob.input();
            ob.calculate();
            ob.display();
        }            
}
Sample Output
Enter automobile no, km and name:
1002
75
Enter Name:
Maruti
Automobile no=1002
Name=Maruti
Rate=95.0



Advance Java Programming


Become A Professional Java Developer From Scratch