[go: up one dir, main page]

0% found this document useful (0 votes)
11 views7 pages

JAVA important question-Answer

This document contains important Java programming questions and answers covering key concepts such as the 'this' keyword, abstract classes and methods, data encapsulation vs. abstraction, packages, exception handling, control statements, and the use of keywords like 'super' and 'final'. Each topic is explained with definitions, roles, and examples to illustrate the concepts. It serves as a comprehensive guide for understanding fundamental Java programming principles.

Uploaded by

roysayanccp05
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views7 pages

JAVA important question-Answer

This document contains important Java programming questions and answers covering key concepts such as the 'this' keyword, abstract classes and methods, data encapsulation vs. abstraction, packages, exception handling, control statements, and the use of keywords like 'super' and 'final'. Each topic is explained with definitions, roles, and examples to illustrate the concepts. It serves as a comprehensive guide for understanding fundamental Java programming principles.

Uploaded by

roysayanccp05
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

JAVA IMPORTANT QUESTIONS-ANSWERS

1. Describe the significance of ‘this’ QUESTIONS-ANSWERS


JAVA IMPORTANT keyword in java programming, explain with
a suitable example.
Ans: 1. Describe the significance of ‘this’ keyword in java
1. To avoid confusion between
programming, explain instance
with variables and parameters (when both
have2.the same name).
2. To call
3. one constructor from another in the same class.
4. anthe
3. To return suitable example.
current class object from a method.
4. To pass the current object as a parameter to another method.
Example:
class Student {
int id;
Student(int id) {
this.id = id;
}
}

2.What are the roles of abstract class & abstract method in java.
Ans:
An abstract class is a class that cannot be directly used to create objects. It is
meant to be inherited by other classes. It can have both abstract methods
(without body) and normal methods (with body).
Role: 1. Used to provide a base structure for other classes.
2.Helps in code reuse and defining common behaviour.

An abstract method is a method without a body (only declaration).


It must be overridden in a subclass.
Role: 1. Forces subclasses to define their own version of the method.
2.Used when some behaviour is common, but the exact implementation
differs in each subclass.
Example:
abstract class Animal {
abstract void sound(); // abstract method
}
class Dog extends Animal {
void sound() {
System.out.println("Barks");
}
}
3.How can you encrypt constructor in multilevel inheritance.
Ans: In Java, we can’t encrypt a constructor, but we can control access in multilevel
inheritance using access modifiers and constructor chaining.
• In multilevel inheritance, each subclass calls its superclass constructor using
super ().
• We can restrict access by using private, protected, or public constructors.
• Protected constructors allow only subclasses or same-package classes to access
them.
Example:
class A {
protected A() { } // Controlled access
}
class B extends A {
protected B() { super(); }
}
class C extends B {
public C() { super(); }
}
4.How instance variable is related with data hiding in java programming?
Ans: In Java, instance variables are often made private to achieve data hiding. This
means they cannot be accessed directly from outside the class.
• Data hiding is a key part of encapsulation, where internal data is hidden from
outside access.
• To access or modify private instance variables, getter and setter methods are
used.
Example:
class Student {
private int marks; // instance variable hidden
public void setMarks(int m) { marks = m; }
public int getMarks() { return marks; }
}
5.Explain the significance of Static Polymorphism.
Ans:
• Increases code readability and flexibility.
• Allows same method name to perform different tasks based on parameters.
• Improves performance as decisions are made at compile time.
Example:
class MathOps {
int add(int a, int b) { return a + b; }
double add(double a, double b) { return a + b; }
}
6.What are the basic differences between data encapsulation and data abstraction.
Feature Data Encapsulation Data Abstraction
Hiding internal
Hiding internal data
Definition implementation, showing
using access modifiers
only features
What functionality is
Focus How data is protected
provided
Achieved Using private variables Using abstract classes or
by + getters/setters interfaces
Protects data from Simplifies usage by hiding
Purpose
outside access complex logic

Example: 1. Encapsulation: private int age; with getAge() method


2. Abstraction: abstract void draw(); in an abstract class
7.What are the importance of package in java programming?
Ans: A package in Java is a group of related classes and interfaces. It helps organize
code in a structured and manageable way.
Importance of Packages:
1. Code Organization – Keeps related classes together, making code easier to
manage.
2. Avoids Name Conflicts – Allows the same class name in different packages.
3. Access Control – Provides protection and control using access modifiers.
4. Reusability – Packages can be reused in other programs.
5. Built-in Packages – Java provides many useful packages like java.util, java.io,
etc.
8.Write a JAVA program to access one package from another package.
// Simulating package1
class Message {
public void showMessage() {
System.out.println("Hello from package1!");
}
}
// Simulating package2
public class Main {
public static void main(String[] args) {
Message msg = new Message();
msg.showMessage();
}
}
Output: Hello from package1!
9. How to package is related to access specifier?
Ans:
1. Default Access (no modifier): Members are accessible only within the same
package.
2. Public: Members are accessible from any package.
3. Protected: Accessible within the same package and in subclasses (even in
different packages).
4. Private: Accessible only within the same class, not even from other classes in
the same package.
Example:

package pack1; package pack2;


public class A { import pack1.A;
public int x = 10;
protected int y = 20; public class B extends A {
int z = 30; // default void show() {
private int w = 40; System.out.println(x); // public
} System.out.println(y); // protected
// System.out.println(z); // default – error
// System.out.println(w); // private – error
}
}

10.Explain the basic concepts of Exception Handelling.


Ans: Exception Handling in Java is used to handle errors or unexpected events
during program execution, without crashing the program.
Basic Concepts:
1. Exception – An error that occurs at runtime (e.g., divide by zero).
2. try block – Code that may cause an exception.
3. catch block – Code that handles the exception.
4. finally block – Code that always runs (optional).
5. throw/throws – Used to manually throw exceptions.
Example:
try {
int a = 10 / 0;
} catch (ArithmeticException e) {
System.out.println("Error: divide by zero");
}
11.Explain the control statement in JAVA & describe the each and every syntax in control
statement.
Ans: In Java, control statements are used to control the flow of execution of the program. They
allow you to make decisions, repeat tasks, or jump to different parts of the code based on
certain conditions.
There are mainly three types of control statements in Java:
Example:

Type Statement Syntax


Decision
if if (condition) { /* code */ }
Making
if-else if (condition) { /* code */ } else { /* code */ }
switch(expression) { case value1: /* code */
switch
break; ... default: /* code */ }
for(initialization; condition; update) { /* code */
Looping for loop
}
while loop while(condition) { /* code */ }
do-while
do { /* code */ } while(condition);
loop
Jump break break;
continue continue;
return return; or return value;

12.WAP in JAVA to demonstrate break & continue statement.


public class BreakContinueDemo { Output:
public static void main(String[] args) { Using continue statement:
System.out.println("Using continue statement:"); i=1
for (int i = 1; i <= 10; i++) {
i=2
if (i == 5) {
continue; // skip the rest of loop when i is 5
i=3
} i=4
System.out.println("i = " + i); i=6
} i=7
System.out.println("\nUsing break statement:"); i=8
for (int i = 1; i <= 10; i++) { i=9
if (i == 5) {
i = 10
break; // exit the loop when i is 5
} Using break statement:
System.out.println("i = " + i); i=1
} i=2
} i=3
} i=4
13.What are the concepts of throw, throws & throwable explain with suitable
example.
Ans:
1. throw: Used to manually throw an exception from a method or block of code.
2. throws: Used in a method declaration to indicate that this method may throw
an exception.
3. throwable: Throwable is the superclass of all errors and exceptions in Java.
Example:
throw:
throw new ArithmeticException("Not eligible");
throws:
void myMethod() throws IOException {
// risky code
}
throwable:
try {
throw new Throwable("Something went wrong");
} catch (Throwable t) {
System.out.println(t.getMessage());
}
14.What is the use of “super” keyword?
Ans: The super keyword is used to refer to the parent class (superclass) from a child
class (subclass).
Main Uses of super:
1. Call parent class constructor
2. Access parent class method
3. Access parent class variable
Example:
class Animal {
String sound = "Generic Sound";
void display() {
System.out.println("Animal makes sound");
}
}
class Dog extends Animal {
String sound = "Bark";
void show() {
System.out.println(super.sound); // Access parent variable
super.display(); // Call parent method
}
}
OUTPUT: Generic Sound
Animal makes sound
15.What are significance of ‘ final’ keyword?
Ans: The final keyword is used to restrict modification. It can be applied to:
Usage Meaning
final variable Value cannot be changed (constant)
final method Method cannot be overridden in a subclass
final class Class cannot be inherited (no subclass can be created)
Example:
final variable:
final int x = 100;
// x = 200; // Error: cannot change value
final method:
class A {
final void show() { }
}
class B extends A {
// void show() { } // Error: cannot override
}
final class:
final class Vehicle { }
// class Car extends Vehicle { } // Error: cannot inherit

16.Explain how do work abstract method and how it is different in normal method
in java programming.
Ans: An abstract method is a method without a body (no implementation). It is
declared with the keyword abstract. It must be defined inside an abstract class.
Subclasses must override it to provide implementation.
Syntax: abstract void show(); // no body
A normal method has a body (implementation). It can be in any class (abstract or
regular). Can be used directly or overridden optionally.
Difference:
Aspect Abstract Method Normal Method
Body No body Has a body
Must use abstract No need for abstract
Declaration
keyword keyword
Inside Class Only in abstract classes In any class
Must be overridden in Can be used directly or
Usage
subclass overridden

You might also like