0% found this document useful (0 votes)
4 views

Java Unit Answers

The document provides an introduction to Java, covering its features, differences with C/C++, and the concepts of JVM, JRE, and JDK. It explains control structures, object-oriented programming principles, exception handling, packages, interfaces, and applet life cycles. Additionally, it includes example programs demonstrating basic Java syntax and functionalities.
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)
4 views

Java Unit Answers

The document provides an introduction to Java, covering its features, differences with C/C++, and the concepts of JVM, JRE, and JDK. It explains control structures, object-oriented programming principles, exception handling, packages, interfaces, and applet life cycles. Additionally, it includes example programs demonstrating basic Java syntax and functionalities.
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/ 5

Unit 1: Introduction to Java

Q: What is Java? Explain features of Java.

A: Java is a high-level, class-based, object-oriented programming language that is

designed to have as few implementation dependencies as possible.

Features: Platform Independent, Object-Oriented, Simple, Secure, Robust,

Multithreaded, Architecture Neutral, Portable, High Performance.

Q: Difference between Java and C/C++.

A: Java is platform-independent, uses a virtual machine, and doesn't support pointers

explicitly, while C/C++ are platform-dependent, compile to native code, and support

pointers.

Q: Explain Java Virtual Machine (JVM), JRE, and JDK.

A: JVM runs Java bytecode, JRE includes JVM and libraries to run Java apps, and JDK

includes JRE plus tools to develop Java apps.

Q: Write a program to display "Hello, World!" in Java.

A: public class HelloWorld {

public static void main(String[] args) {

System.out.println("Hello, World!");

Q: Explain data types and variables in Java with examples.

A: Data types: int, float, double, char, boolean, etc.

Example: int x = 10; double y = 5.5; char c = 'A'; boolean flag = true;

Unit 2: Control Structures & Arrays


Q: Explain if-else, switch-case, while, do-while, and for loops with syntax and examples.

A: if-else: if (condition) {...} else {...}

switch-case: switch (var) { case val: ... break; ... }

while: while(cond) {...}


do-while: do {...} while(cond);

for: for(init; cond; inc) {...}

Q: Write a Java program to find the largest of three numbers using if-else.

A: int a=10, b=20, c=15;

if(a>=b && a>=c) System.out.println(a);

else if(b>=a && b>=c) System.out.println(b);

else System.out.println(c);

Q: Explain break and continue statements with examples.

A: break exits a loop; continue skips to next iteration.

Example:

for(int i=0; i<5; i++) { if(i==3) break; }

for(int i=0; i<5; i++) { if(i==2) continue; }

Q: What are arrays? Write a program to find the sum of array elements.

A: Array is a collection of similar data types.

int[] arr = {1,2,3}; int sum = 0;

for(int i: arr) sum += i;

System.out.println(sum);

Unit 3: Object-Oriented Programming


Q: Explain OOP principles: Encapsulation, Inheritance, Polymorphism, Abstraction.

A: Encapsulation: binding data/methods.

Inheritance: acquire properties.

Polymorphism: many forms.

Abstraction: hiding internal details.

Q: Define class and object in Java. Write a program to create a class and object.

A: class Car { int speed; }

public class Main {

public static void main(String[] args) {


Car c = new Car();

Q: What is constructor? Types of constructors.

A: Constructor initializes objects. Types: Default, Parameterized, Copy constructors.

Q: Explain method overloading and method overriding with examples.

A: Overloading: same method name, different params.

Overriding: subclass method redefines parent method.

Q: Write a program to implement single inheritance.

A: class A { void show(){} }

class B extends A { void display(){} }

Unit 4: String Handling and Exception


Q: Difference between String and StringBuffer.

A: String is immutable, StringBuffer is mutable and thread-safe.

Q: Write a program to reverse a string.

A: String s = "hello";

StringBuilder sb = new StringBuilder(s);

sb.reverse();

System.out.println(sb);

Q: What is exception handling? Explain try-catch-finally.

A: Mechanism to handle runtime errors. try contains code, catch handles exception,

finally executes always.

Q: Write a program to handle divide-by-zero exception.

A: try { int a = 5/0; } catch(ArithmeticException e) { System.out.println(e); }

Q: Explain built-in exceptions in Java.

A: Examples: ArithmeticException, NullPointerException,

ArrayIndexOutOfBoundsException, NumberFormatException.
Unit 5: Packages and Interfaces
Q: What is a package? How to create and use it?

A: Package is a namespace. Use 'package pkgName;' and import it using 'import

pkgName.*';

Q: Define interface. Difference between class and interface.

A: Interface is a reference type with abstract methods. Interface can’t have method

bodies (till Java 7).

Q: Write a program to implement an interface.

A: interface Animal { void sound(); }

class Dog implements Animal {

public void sound() { System.out.println("Bark"); }

Q: Difference between abstract class and interface.

A: Abstract class can have method bodies and constructors; interface can’t (till Java 7),

supports multiple inheritance.

Unit 6: Applet and AWT


Q: What is an Applet? How is it different from an application?

A: Applet runs in browser, no main method; application runs standalone.

Q: Explain the life cycle of an Applet.

A: init(), start(), paint(), stop(), destroy()

Q: Write a simple Applet program.

A: public class MyApplet extends Applet {

public void paint(Graphics g) {

g.drawString("Hello Applet", 50, 50);

Q: Explain AWT controls like Button, Label, TextField, etc.


A: AWT provides UI components: Button b = new Button("Click"); Label l = new

Label("Name"); TextField tf = new TextField();

You might also like