ISC Q/ANS

  1. Why do we write throws IOException after main()
    throws keyword followed by the exception name i.e. IOException in this case, is used to handle anomalous situations in a program. Throwing exceptions over a function body helps in preventing/correcting possible errors in java programs.
  2. What is an Object Oriented Programming (OOP) Language?
    An OOP language is one which follows OOP paradigm. An OOP language like C++, Java, PHP etc consists of classes which have data members and member functions. These languages use the OOP concepts ofAbstraction, Encapsulation, Polymorphism and Inheritance.
  3. What is data abstraction?
    Data Abstraction refers to the act of representing essential features without showing background details or explanations. It can be achieved by making use of private/protected methods in a class.
  4. What is encapsulation?
    Encapsulation is the wrapping up of data(data members) and functions(methods) into a single unit/block. A class is the example of using the concept of encapsulation.
  5. What is Inheritance? State different forms of Inheritance.
    The ability of a class to acquire certain characteristics(data members) or behaviour(methods) of another class is known as Inheritance. The class which derives data from another class is known as the derived or sub class and the class from which it derives is known as base or super class. The Different forms of inheritance are: Single, Multiple, Multilevel, Hybrid and Hierarchical.
  6. What is Polymorphism? How is it implemented?
    The ability of the data to be perceived in more than one form is known as polymorphism. It is implemented using function(constructor) overloading and operator overloading.
  7. Difference between for-loop and while-loop.
    For loop runs for a fixed number of times whereas while loop runs as long as the given condition is true.
  8. Difference between while-loop and do-while loop.
    Unlike while loop, a do-while loop runs as long as the given condition is true.
  9. Difference between iteration and recursion.
    In iteration, memory space once allocated is used repeatedly for computation whereas in recursion new memory space is allocated for each function call. This makes recursion slower than iteration.
  10. Difference between class and interface.
    class consists of data members and functions which are defined using these data members. Whereas, an interfaceconsists of only function prototypes with no definition. The class which implements interface agrees to provide definition for all of its methods. An interface cannot be instantiated like classes. By default, the members of an interface are abstract andpublic.
  11. Difference between interface and abstract class.
    Abstract Class
    Abstract classes are used to declare common characteristics of subclasses. An abstract class cannot be instantiated. It can only be used as a super class for other classes that extend the abstract class. Abstract classes are declared with the keyword abstract.

    Interface
    An Interface is used to define a generic template. Interfaces just specify the method declaration (implicitly public and abstract) and can only contain fields (which are implicitly public static final). Interface definition begins with a keyword interface. An interface may never contain method definitions. An interface cannot be instantiated. Multiple Inheritance is facilitated through interfaces in Java. A class that implements an interface must implement all of the methods described in the interface, or be an abstract class.
  12. Difference between throw and throws keywords.
    1. Throws clause in used to declare an exception and throw keyword is used to throw an exception explicitly.
    2. If we see syntax wise than throw is followed by an instance variable andthrows is followed by exception class names.
    3. The keyword throw is used inside method body to invoke an exception and throws clause is used in method declaration (signature).
    4. The keyword throw is used inside method body to invoke an exception and throws clause is used in method declaration (signature). For example:

          Throw:
      
          ....
          static{
          try {
          throw new Exception("Something went wrong!!");
          } catch (Exception exp) {
          System.out.println("Error: "+exp.getMessage());
          }
          }
      
          ....
          Throws:
      
          public void sample() throws ArithmeticException{
          //Statements
      
          .....
      
          //if (Condition : There is an error)
          ArithmeticException exp = new ArithmeticException();
          throw exp;
          ...
          }
                          
    5. By using Throw keyword in java you cannot throw more than one exception but using throws you can declare multiple exceptions. For example:

          Throw:
      
          throw new ArithmeticException("An integer should not be divided by zero!!")
          throw new IOException("Connection failed!!")
      
          Throws:
      
          throws IOException, ArithmeticException, NullPointerException,
          ArrayIndexOutOfBoundsException
                          
  13. What does import java.io.* mean?
    import keyword is used to include a package in a program.
    java.io is a pre-defined package in java that contains classes required in input/output (I/O) operations. *(asterisk) here acts as the wildcard i.e. all classes within the java.io package are imported.
  14. Explain the line public static void main(String args[]).
    public meqdesc the function main can be accessed from anywhere i.e. its access is not limited.
    static meqdesc the function can be called using its class name and there is no need to create an object of its class to call it.
    void meqdesc null/empty. It meqdesc that the function is not returning any value.
    main() is the function present in every java program. Every java program starts and ends within the main.
  15. What is constructor overloading?
    Constructors are functions with the same name as that of its class. When several constructors with different signature are defined it is known as constructor overloading.
  16. What is a copy constructor?
    A constructor which receives an object is known as a copy constructor.
  17. What is the use of this keyword?
    this keyword is used to access the data members of a class.
  18. What is the difference between overloading and overriding?
    Overloading uses the concept of polymorphism where multiple functions with same name are allowed provided their signatures are different.
    A method in a subclass hides or overshadows a method inherited from the super class if both methods have the same name and signature. This property is known as overriding the inherited method.
  19. Explain super keyword.
    super keyword is used to access the members of base class from a derived class in inheritance.

  20. Explain recursive case and base case.
    recursive case consists of the problem that is to be solved. It contains the logic to solve a problem along with the function call. base case is the part of the problem whose solution is known to us.
    For ex:- To calculate factorial of a number
    we know that
    0!=1
    1!=1
    this is the base case.
    Also, the factorial of a number 'n' is calculated as
    n!= n x (n-1)!
    this part is known as the recursive case.
  21. What is fall-through condition in switch-case?
    When break statement is missing in a switch statement, the control falls to the cases following the matched case. This is known as fall-through condition.
  22. What are wrapper classes?
    The classes corresponding to the primitive data types are known as wrapper classes. For ex:- For primitive data type int we have Integer class. Wrapper classes contain functions which are used in data manipulation.

BLUEJ THEORY 3

this keyword is used to refer to the object on which a method was invoked.
Take a look at the following code which contains a single instance variable x and a method setX().
1public class MyNumber {
2 
3    int x;
4 
5    public void setX() {
6        this.x = 3;
7    }
8}
Now, to understand what this.x means, we use the following code
1public class Test {
2 
3    public static voidmain(String[] args) {
4        MyNumber myNumber1 =new MyNumber();
5        myNumber1.setX();
6    }
7}
The method setX() was called on the object myNumber1. Within setX(), the statement this.x=3 will be executed. As we said in the beginning of this article, ‘this refers to the object on which a method was invoked’. Here, we have invoked the method setX() on the object myNumber1. So, this will refer to the object myNumber1. So,
this.x = 3 ;
will be interpreted as
myNumber1.x = 3
which will set x to 3.
We could written the above program without using this keyword in the following way.
1public class MyNumber {
2 
3    int x;
4 
5    public void setX() {
6        x = 3;
7    }
8}
The output will still be the same.
Now, you might ask what is the use of this keyword if we can achieve the same thing without using it.
Consider the following example:
1public class MyNumber {
2 
3    int x = 3;
4 
5    public void printX() {
6        int x = 4;
7        System.out.printn(x);
8    }
9}
The method printX() will print 4 and not 3. This is because when we declare a variable in a method with the same name as that of another instance variable, then when we refer to that variable within the method, we get access to the variable defined in the method and not the instance variable of the class. This is illustrated in the following diagram:
If we want to access the instance variable, we do it with this.x.
1public class MyNumber {
2 
3    int x = 3;
4 
5    public void setX() {
6        x = 4;
7        System.out.printn(x);
8        System.out.printn(this.x);
9    }
10}
The above program will print,
14
23
To see why the second line printed 3, let us use the first line in this article – ‘this refers to the object on which the method was invoked’.
1MyNumber myNumber1 = newMyNumber();
2myNumber1.printX();
The method printX() was invoked on the object MyNumber1. So, this will refer to myNumber1 and this.x will be myNumber1.x which is 3.
Normally, we use the ‘this’ keyword in constructors and set methods so that we can use the same name for instance variables and formal parameters.
Here is an example program with the keyword this used in a constructor and two set methods.
1public class Student {
2 
3    int rollNumber;
4    int marks;
5 
6    public Student(introllNumber, int marks) {
7        this.rollNumber = rollNumber;
8        this.marks = marks;
9    }
10 
11    public voidsetRollNumber(int rollNumber) {
12        this.rollNumber = rollNumber;
13    }
14 
15    public void setMarks(intmarks) {
16        this.marks = marks;
17    }
18}

Second largest number in java using ternary operator

 //Largest second number using ternary operator public class Main { public static void main(String[] args) { int a=5,b=6,c=7; int ...