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.

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 ...