Java Ch1 Ch2 Short Answer Answers
Java Ch1 Ch2 Short Answer Answers
Answer: 1. Java is a high-level, object-oriented programming language originally named 'Oak' by its
Answer: 2. Three features of Java are: Platform Independence (via JVM), Object-Oriented
Answer: 3. JVM (Java Virtual Machine) executes bytecode and enables Java programs to run on
Answer: 4. JDK (Java Development Kit) includes tools to develop Java programs. JRE (Java
Answer: 5. 'javac' compiles Java source files into bytecode. 'java' runs the compiled bytecode via the
JVM.
Answer: 6. Java supports single-line (//), multi-line (/* */), and documentation (/** */) comments for
Answer: 7. Java has 8 primitive data types: byte, short, int, long, float, double, boolean, and char.
Answer: 8. Primitive variables store actual values; reference variables store memory addresses of
objects.
Answer: 9. Variable names must start with a letter, underscore, or dollar sign and can't contain
Answer: 11. public class HelloWorld { public static void main(String[] args) {
System.out.println("Hello World"); } }
Answer: 12. '=' is an assignment operator; '==' checks equality between values.
Answer: 13. Control statements: Selection (if, switch), Looping (for, while, do-while), and Jumping
Answer: 14. The if-else-if ladder checks multiple conditions in sequence and executes the matching
block.
Answer: 16. A class is a blueprint for creating objects. Components include fields, methods,
Answer: 19. A constructor initializes objects and has no return type. A method defines behavior and
Answer: 20. Method overloading allows multiple methods with the same name but different
parameters.
Answer: 21. 'this' refers to the current object and is used to resolve naming conflicts between
Answer: 22. Instance variables are unique to each object. Class (static) variables are shared among
all instances.
Answer: 23. Static methods are declared with the static keyword and are accessed using the class
name.
Answer: 24. Access modifiers: public (accessible everywhere), private (within class), protected
Answer: 25. public class Student { String name; int id; Student(String n, int i) { name = n; id = i; } }
Answer: 26. Class variables are accessed using ClassName.variableName or through any instance
(not recommended).
Answer: 27. A NullPointerException occurs because the object elements in the array are not
instantiated.
28. Explain the difference between local, instance, and class variables.
Answer: 28. Local variables exist inside methods, instance variables belong to objects, and class
29. What are the steps involved in creating and using an object?
Answer: 29. Define a class, declare an object, use 'new' to instantiate it, and then access members
Answer: 30. Java secures code using JVM, bytecode verification, and the class loader to prevent