Java Important Questions & Answers PART B (4 Marks) 1.
List the features of Java
programming Language.
- Simple, Object-Oriented, Platform Independent, Robust & Secure, Multithreaded, Distributed,
Portable & High Performance.
2. Define a class and object. Write syntax to create class and object with an Example in java.
Class: Blueprint/template that defines properties and behaviors.
Object: Instance of a class.
class Student { String name; void display() { [Link]("Name: " + name); } } public class
Main { public static void main(String[] args) { Student s = new Student(); [Link] = "John";
[Link](); } }
3. Implement a java program to display all the odd numbers between 1 to 30 using for loop &
if statement. public class OddNumbers { public static void main(String[] args) { for (int i = 1; i <= 30;
i++) { if (i % 2 != 0) { [Link](i + " "); } } } }
4. Discuss on abstract class with an example.
Abstract class is declared with abstract keyword. It can have abstract (without body) and concrete
methods. Cannot be instantiated, must be inherited.
abstract class Animal { abstract void sound(); void sleep() { [Link]("Sleeping..."); } }
class Dog extends Animal { void sound() { [Link]("Bark"); } }
5. Explain why java is platform independent language?
Java source code is compiled into bytecode which runs on any machine having JVM. This is called
Write Once Run Anywhere (WORA).
6. Discuss how run-time polymorphism is achieved in java.
Achieved through method overriding. Decision which method to call is made at runtime (dynamic
method dispatch).
7. Compare abstract class and interface.
Abstract class: Can have abstract + concrete methods, instance variables, supports single
inheritance.
Interface: Only abstract methods (Java 7), supports multiple inheritance.
8. Discuss on different access specifier in java.
Public - Accessible everywhere, Private - Within same class, Protected - Same package +
subclass, Default - Same package only.
PART C (12 Marks) 1. Develop a java program to implement multilevel inheritance with 4
levels of hierarchy. class A { void displayA() { [Link]("Class A"); } } class B extends A {
void displayB() { [Link]("Class B"); } } class C extends B { void displayC() {
[Link]("Class C"); } } class D extends C { void displayD() { [Link]("Class
D"); } } public class MultilevelInheritance { public static void main(String[] args) { D obj = new D();
[Link](); [Link](); [Link](); [Link](); } }
2. Explain method overriding with suitable example. class Animal { void sound() {
[Link]("Animal makes sound"); } } class Dog extends Animal { void sound() {
[Link]("Dog barks"); } } public class Test { public static void main(String[] args) { Animal
a = new Dog(); [Link](); } }
3. Present the importance of super and this keyword in inheritance.
super: Refers to parent class object, used to call parent class constructor and methods.
this: Refers to current class object, used to call current class methods and variables.
class Parent { Parent() { [Link]("Parent constructor"); } } class Child extends Parent {
Child() { super(); [Link]("Child constructor"); } }
4. Discuss how interface is used to achieve multiple inheritances in Java. interface A { void
displayA(); } interface B { void displayB(); } class C implements A, B { public void displayA() {
[Link]("A method"); } public void displayB() { [Link]("B method"); } }
5. Show how to create and access user defined package in Java with example.
Step 1: Create package mypack
package mypack; public class Message { public void show() { [Link]("Hello from
mypack"); } } Step 2: Use package
import [Link]; public class Test { public static void main(String[] args) { Message m =
new Message(); [Link](); } }
6. Explain Function Definition & Function Argument with example.
Function Definition: Block of code that performs a task.
void greet() { [Link]("Hello"); } Function Argument: Values passed to function.
void greet(String name) { [Link]("Hello " + name); } greet("John");
7. How to create a Package in Java? Explain with example.
Step 1: Create package with package keyword.
package mypack; public class A { public void show() { [Link]("Inside package"); } } Step
2: Compile with javac -d . [Link]
Step 3: Use package
import mypack.A; class Test { public static void main(String[] args) { A obj = new A(); [Link](); } }