0% found this document useful (0 votes)
2 views10 pages

JAVA Ans

The document provides examples of various Java programming concepts including data types, arrays (single and two-dimensional), autoboxing and unboxing, control statements (if, switch, loops), constructors (default and parameterized), and inheritance (single, multi-level, and hierarchical). Each section includes code snippets demonstrating the concepts in action. Overall, it serves as a comprehensive guide to fundamental Java programming principles.

Uploaded by

bhoomikanavalyal
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)
2 views10 pages

JAVA Ans

The document provides examples of various Java programming concepts including data types, arrays (single and two-dimensional), autoboxing and unboxing, control statements (if, switch, loops), constructors (default and parameterized), and inheritance (single, multi-level, and hierarchical). Each section includes code snippets demonstrating the concepts in action. Overall, it serves as a comprehensive guide to fundamental Java programming principles.

Uploaded by

bhoomikanavalyal
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/ 10

1.

Data type
public class EasyDatatype {
public sta c void main(String[] args) { Integer: 100
int a = 100; Float: 12.2
float f = 12.2f; Long: 120000
long l = 120000; Short: 500
short s = 500; Double: 12.321
double d = 12.321; Character: a
char c = 'a'; Boolean: true
boolean b = true; Byte: 4
byte k = 4; String: Kiran
String name = "Kiran";

System.out.println("Integer: " + a);


System.out.println("Float: " + f);
System.out.println("Long: " + l);
System.out.println("Short: " + s);
System.out.println("Double: " + d);
System.out.println("Character: " + c);
System.out.println("Boolean: " + b);
System.out.println("Byte: " + k);
System.out.println("String: " + name);
}
}

2. (a) Single dimensional array


Package testarray;
class TestArray { 10
public sta c void main(String[] args) { 20
int a[] = new int[5]; 30
a[0] = 10; 40
a[1] = 20; 50
a[2] = 30;
a[3] = 40;
a[4] = 50;

for (int i = 0; i < a.length; i++) {


System.out.println(a[i]);
}
}
}
2. (b)Two- dimensional array
package twodim;
public class TwoDim { 258
public sta c void main(String[] args) { 486
int[][] a = {{1,3,4},{2,4,3},{3,4,5}}; 469
int[][] b = {{1,2,4},{2,4,3},{1,2,4}};
int[][] c = new int[3][3]; // to store result
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
c[i][j] = a[i][j] + b[i][j];
System.out.print(c[i][j] + " ");
}
System.out.println();
}
}
}

3. (a)Autoboxing
package autoboxing;
public class Autoboxing { Primi ve: 10
public sta c void main(String[] args) { Boxed (Integer): 10
int num = 10; // primi ve int
Integer boxedNum = num; // autoboxing happens
here

System.out.println("Primi ve: " + num);


System.out.println("Boxed (Integer): " + boxedNum);
}
}

3. (b)Unboxing
public class EasyUnboxing {
public sta c void main(String[] args) { 100
Integer num = 100; // Autoboxing
int value = num; // Unboxing
System.out.println(value);
}
}
4. Different control statements
a. If statement
package basics;
public class IfStatement { Number is posi ve
public sta c void main(String[] args) {
int num = 5;
if (num > 0) {
System.out.println("Number is posi ve");
}
}
}

b. If else
package basics;
Nega ve number
public class IfElse {
public sta c void main(String[] args) {
int num = -3;
if (num >= 0) {
System.out.println("Posi ve number");
} else {
System.out.println("Nega ve number");
}
}
}

c. If else if
package basics;
public class IfElseIf { Zero
public sta c void main(String[] args) {
int num = 0;
if (num > 0) {
System.out.println("Posi ve");
} else if (num < 0) {
System.out.println("Nega ve");
} else {
System.out.println("Zero");
}
d. Nested if
package basics;
Posi ve even number
public class NestedIf {
public sta c void main(String[] args) {
int num = 10;
if (num > 0) {
if (num % 2 == 0) {
System.out.println("Posi ve even number");
}
}
}
}

e. Switch case
package basics;
Tuesday
public class SwitchCase {
public sta c void main(String[] args) {
int day = 2;
switch (day) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
default:
System.out.println("Other day");
}
}
}

f. While loop
package basics;
i=1
public class WhileLoop {
public sta c void main(String[] args) { i=2
int i = 1; i=3
while (i <= 3) {
System.out.println("i = " + i);
i++;
}
}
}
g. Do while
package basics;
public class DoWhileLoop { i=1
public sta c void main(String[] args) {
i=2
int i = 1;
do { i=3
System.out.println("i = " + i);
i++;
} while (i <= 3);
}
}

h. For loop
package basics;
public class ForLoop { i=1
public sta c void main(String[] args) { i=2
for (int i = 1; i <= 3; i++) {
System.out.println("i = " + i); i=3
}
}
}

i. Break statement
package basics;
public class BreakStatement { i=1
public sta c void main(String[] args) {
for (int i = 1; i <= 5; i++) { i=2
if (i == 3) {
break;
}
System.out.println("i = " + i);
}
}
}

j. Con nue statement


package basics;
public class Con nueStatement {
i=1
public sta c void main(String[] args) {
for (int i = 1; i <= 5; i++) { i=2
if (i == 3) {
con nue; i=4
} i=5
System.out.println("i = " + i);
}
}
}

k. Return statement
public class Main {
public sta c int getNumber() { Returned value: 5
return 5; // returns the number 5
}
public sta c void main(String[] args) {
int result = getNumber();
System.out.println("Returned value: " + result);
}
}

5. Answers is same as 2nd one

6. Expression evalua on
package ExpressionDemo;
Value of x is: 1036.0
public class ExpressionDemo {
public sta c void main(String[] args) {
float x = (20 * 5 * 10) + (30 / 5) - (10 % 2) + (3 * 10);
System.out.println("Value of x is: " + x);
}
}

7. (a) Default constructor


package constructor;
class Box { Volume is: 6000
long width;
long height;
long depth;
Box() {
width = 10;
height = 20;
depth = 30;
}
void volume() {
long v = width * height * depth;
System.out.println("Volume is: " + v);
}
}

public class Constructor {


public sta c void main(String[] args) {
Box b = new Box();
b.volume();
}
}

7. (b)Parameterized constructor
class Student {
String name; Name: Rahul
int age; Age: 20

// Parameterized constructor
Student(String n, int a) {
name = n;
age = a;
}

void display() {
System.out.println("Name: " + name);
System.out.println("Age: " + age);
}
}

public class ConstructorExample {


public sta c void main(String[] args) {
Student s1 = new Student("Rahul", 20);
s1.display();
}
}
8. (a) Single inheritance
package singleinheritance;
class Parent { Parent class
void pmethod() {
System.out.println("Parent class"); Child class
}
}
class Child extends Parent {
void cmethod() {
System.out.println("Child class");
}
}

public class SingleInheritance {


public sta c void main(String[] args) {
Child c = new Child();
c.pmethod();
c.cmethod();
}
}

8. (b) Mul level inheritance


package vehicles;
class Vehicle { Electric car needs to be charged
public void drive() {
System.out.println("Vehicle is driving"); Car needs fuel
} Vehicle is driving

}
class Car extends Vehicle {
public void fuel() {
System.out.println("Car needs fuel");
}
}
class ElectricCar extends Car {
public void charge() {
System.out.println("Electric car needs to be charged");
}
}

public class Mul LevelInheritanceExample {


public sta c void main(String[] args) {
ElectricCar myCar = new ElectricCar();
myCar.charge();
myCar.fuel();
myCar.drive();
}
}
8.(c) Hierarchical inheritance
package hierarchicaldemo;
class A { I'm in C
public void aMethod() {
I'm in A
System.out.println("I'm in A");
} I'm in B
}
class B extends A {
public void bMethod() {
System.out.println("I'm in B");
}
}
class C extends B {
public void cMethod() {
System.out.println("I'm in C");
}
}
public class HierarchicalDemo {
public sta c void main(String[] args) {
C obj = new C();
obj.cMethod();
obj.aMethod();
obj.bMethod();
}
}

You might also like