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

JAVA file

The document contains Java programming exercises with solutions for printing 'Hello World', adding two integers, and representing local, instance, and static variables. Each exercise includes the code implementation and expected output. The examples demonstrate basic Java concepts and syntax.

Uploaded by

Manmeet Singh
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

JAVA file

The document contains Java programming exercises with solutions for printing 'Hello World', adding two integers, and representing local, instance, and static variables. Each exercise includes the code implementation and expected output. The examples demonstrate basic Java concepts and syntax.

Uploaded by

Manmeet Singh
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

JAVA

Q1. Write a program in Java to print “Hello World”.


Sol.
public class Main{
public static void main(String[] args) {
System.out.println("Hello World");
}
}
OUTPUT:
Q2: Write a program in Java for addition of two integers.
Sol.
public class Main{
public static void main(String[] args) {
int num1 = 10;
int num2 = 5;
int sum = num1 + num2;

System.out.println("The sum is: " + sum);


}
}
OUTPUT:

Q3: Write a program in java to represent Local Variables.


Sol.
public class Main{
public static void main(String[] args) {
int number = 10;
String message = "Hello Java";

System.out.println("Number: " + number);


System.out.println("Message: " + message);
}
}
OUTPUT:

Q4: Write a program in java to represent Instance Variables.


Sol. public class Main {
String name;
int age;
public static void main(String[] args) {
Main p = new Main();
p.name = "Prabhpreet";
p.age = 18;
System.out.println("Name: " + p.name);
System.out.println("Age: " + p.age);
}
}

Q5: Write a program in java to represent Static Variables.


Sol.
public class Main {
static String schoolName = "SGTBIMIT";\

public static void main(String[] args) {


System.out.println("School Name: " +
Main.schoolName);
}
}
OUTPUT:

You might also like