Java Question – I (Answers)

  1. What is JDK?
    1. Java Development Kit – The java installation package for software development.
  2. What is javac?
    1. Java Compiler program to compile the java source code
  3. What is JRE?
    1. Java Runtime Engine – This is the package, which is included in the JDK along with javac, which is used to run the java programs.
  4. What is JVM?
    1. Java Virtual Machine – This is the operating system specific executable, which is used to execute the compiled java programs. On Windows this is “java.exe”, while on Linux/Mac, this is “java” executable.
  5. What does a .java file contain?
    1. Java source code is written in .java files.
  6. What does a .class file contain?
    1. The output of the Java compilation output is in the bytecode format in files with the .class extension.
  7. Why is java considered platform independent?
    1. The java source code can be compiled on any platform, and the bytecode in the class files can be run on any platform, using the platform specific JVM. This is the reason java is considered platform independent.
  8. What is difference of a primitive and non-primitive data type?
    1. The fundamental data types are termed as primitive data types. These are the only items in java that are not classes. Example of primitive data types are boolean, char, byte, short, int, long, float and double. Non-primitive data types include Classes, Interfaces, and Arrays.
  9. Give a short example code snippet for each of the two conditional statements in Java?
    1. if (val == true) { do_something(); }
    2. switch (some_int_var) {
      case 10: do_something1(); break;
      case 20: do_something2(); break;
      default: do_default(); break;
      }
  10. Give a short example code snippet for two loop statements in Java?
    1. while (i < 5) { j = j + 1; i–; }
    2. for (i=0; i<5; i++) { j = j + 1; }
  11. What are the components of a method “signature”?
    1. A method signature includes 0 or more arguments and return type (which can be void, signifying no return)
  12. What is OOP?
    1. Object Oriented Programming
  13. List at least 4 differences between OOP and Procedural Programming?
    1. OOP vs Procedural ==
      Bottom Up Approach vs Top Down Approach.
      No memory management required
      Secure due to access management (private vs public etc.)
      Object Oriented Principles help program growth.
  14. What are the 4 pillars or 4 main components of OOP?
    1. IPEA = Inheritance     Polymorphism    Encapsulation    Abstraction
  15. How would you define an object?
    1. An object is a software representation of a real world entity. Example of objects are “man, woman, child” or “cat, dog, horse” or “car, truck, tractor”
  16. What are the two components of an object?
    1. An object has attributes/properties that defines the characteristics of the object and methods/behaviors that defines the operations that can be on the object. E.g man object: properties = (name=manoj, age=50) etc. behaviors = (walk, run) etc.
  17. How would you define a class?
    1. A class is a textual template representation of the attributes and behaviors of an object.
  18. What is the relationship between a class and an object?
    1. An object is “an instance of a class template” with different values for the various attributes.
  19. What are object attributes & properties?
    1. Attributes and Properties are the same, and define the characteristics of an object.
  20. What are object methods & behaviors?
    1. Methods and Behaviors are the same, and define the operations that can be done on an object.
  21. What is the difference between a class attribute and an object (or instance) attribute?
    1. An object attribute defines the characteristics of a single object instance. A class attribute (defined using the static keyword), defines a variable which is “shared” by all the object instances. So, if a class variable is changed by any object instance, it is changed for ALL instances.
  22. How would you create an instance of an object “o1″ from a class “CCC”?
    1. CCC o1 = new CCC();
  23. What is the significance of a class constructor?
    1. When a new object is created, the constructor method of the class is called implicitly. It is typically used to initialize the object attributes with default values. A constructor can also accept arguments. If there is no explicit constructor defined, an empty constructor with no arguments is called.
  24. What is the significance of a class destructor?
    1. When a class is “destroyed” (using the statement “delete o1;”), the class destructor is implicitly called (just like the constructor). A typical use of a destructor is to close files or databases opened in the object. If there is no explicit destructor defined, an empty destructor is called, that does nothing. Unlike a constructor, a destructor cannot accept any arguments.
  25. What do you mean by Inheritance?
    1. Inheritance is when you have a top level parent class, with certain generic attributes and methods, and you have child classes which are derived from this parent class. All generic attributes and methods from the parent class, are automatically part of the child class, without having to rewrite the code. This is especially useful when you have a single parent class (example: Mammal) that has many child classes (e.g. Man, Dog, Horse) which share certain common attributes (age, height, color) and methods (walk(), breathe()), but the children are different from each other to need their own additional attributes and methods. Inheritance is either single inheritance (one parent has only one child class), multiple inheritances  (one parent has one child, and child has one grand-child) and hierarchical inheritance (one parent has multiple children).
  26. How would you inherit a child class C1 from a parent class P1?
    1. class C1 extends P1 {
      };
  27. What do you mean by Polymorphism?
    1. Polymorphism is when you have multiple methods in a class (or sub-class) with the exact same name, but different signatures (method overloading), or method in a sub-class with the exact name name AND signature as its parent (method overriding). This is useful so the same behavior can be invoked for an object, and the correct method associated with the object is called (either the base method from the parent if there is no overloaded or overridden method) or the appropriate overloaded/overridden method.
  28. What is the difference between overriding and overloading of methods?
    1. Overloaded methods have the same name, but different signatures.
      Overridden methods have the same name AND the same signature.
      Overloaded methods can be defined in the same class, or in a sub-class.
      Overridden methods can only be defined in sub-classes (derived classes).
  29. What is the difference between static polymorphism and run-time polymorphism?
    1. In case polymorphism is implemented, if the decision of which method to call for a specific object is made during compile time, it is termed “static”. This is possible for overloaded methods (with different signatures). On the other hand, for overridden methods, the decision can only be made after the object instances have been created during program execution, and are thus defined as run-time polymorphism.
  30. What do you mean by Encapsulation?
    1. While object attributes can always be made public, such that they are accessible via every object instance, it is better to limit the visibility of the attributes and methods such that attributes that are only to be manipulated by the class methods, cannot be changed externally (i.e. they are encapsulated within the object scope.). The keywords (private and public) define this encapsulation behavior for an object.
  31. What is the difference between private scope attribute and public scope attribute?
    1. (ANSWERS AFTER THIS TOMORROW)
  32. Why would you create a protected scope attribute?
  33. What do you mean by Abstraction?
  34. In Java, what is the difference between the String and StringBuffer class?
  35. What is the scope of a local variable vs an instance variable?
  36. What is the significance of the “this” parameter?
  37. What is the significance of the “super” parameter?
  38. What are the wrapper classes for the primitive data types?
  39. What is an array?
  40. (Not a question) An array is an object that contains a collection of elements of the same type, indexed by a number starting with 0. An array variable is a reference to an array object.
  41. Write the code snippet to declare a variable “a” as an integer array that can hold 5 integer values?
  42. What are the three java Collection classes? Give an example where each would be used?
  43. What is the difference between abstract class vs interfaces?
  44. What is a recursive function?

Leave a Reply