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

1.variables and Data Types

The document discusses Java concepts including JDK, JRE, JVM, primitive data types in Java, variable naming rules, and examples of simple Java programs to calculate area of shapes, perform arithmetic operations, swap values, calculate simple interest, and more.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

1.variables and Data Types

The document discusses Java concepts including JDK, JRE, JVM, primitive data types in Java, variable naming rules, and examples of simple Java programs to calculate area of shapes, perform arithmetic operations, swap values, calculate simple interest, and more.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Variables and Data Types

1. Explain JDK, JRE and JVM?


JDK:
JDK is an acronym for Java Development Kit. The Java Development Kit (JDK) is a
software development environment which is used to develop Java applications and
applets. It physically exists. It contains JRE + development tools. The JDK contains a
private Java Virtual Machine (JVM) and a few other resources such as an
interpreter/loader (java), a compiler (javac), an archiver (jar), a documentation generator
(Javadoc), etc. to complete the development of a Java Application.
JRE:
JRE is an acronym for Java Runtime Environment. It is also written as Java RTE. The
Java Runtime Environment is a set of software tools which are used for developing Java
applications. It is used to provide the runtime environment. It is the implementation of
JVM. It physically exists. It contains a set of libraries + other files that JVM uses at
runtime. The implementation of JVM is also actively released by other companies
besides Sun Micro Systems.
JVM:
JVM (Java Virtual Machine) is an abstract machine. It is called a virtual machine
because it doesn't physically exist. It is a specification that provides a runtime
environment in which Java bytecode can be executed. It can also run those programs
which are written in other languages and compiled to Java bytecode. The JVM performs
the following main tasks: o Loads code o Verifies code o Executes code o Provides
runtime environment

2. Why Java is platform independent?


Java is called Platform Independent because programs written in Java can be run on
multiple platforms without re-writing them individually for a particular platform, i.e.
Write Once Read Anywhere.

3. List down all primitive data types in java in order of their capacity.

Data Type Default value Default size


Boolean false 1bit
Char \u0000 2 byte
Byte 0 1 bit
Short 0 2 byte
Int 0 4 byte
Long 0L 8 byte
Float 0.0f 4 byte
Double 0.0d 8 byte
4. Which of the following are valid variable names?
Rules:

 Names can contain letters, digits, underscores, and dollar signs


 Names must begin with a letter
 Names should start with a lowercase letter and it cannot contain whitespace
 Names can also begin with $ and _ (but we will not use it in this tutorial)
 Names are case sensitive ("myVar" and "myvar" are different variables)
 Reserved words (like Java keywords, such as int or boolean) cannot be used as names

a. _$_$_$ - valid b. my$Var - not valid


c. 1_emp_no - valid d. good@str - valid
e. $ 9 - valid f. V$A$V – not valid

5. Write a java program to calculate area of circle.


public class AreaOfCircle {

public static void main(String[] args) {

int radius=20;
double pi = 3.14, Area;
Area = pi * radius * radius;
System.out.println("Area of circle:"+Area);
}
}
Output:

6. Write a Java program to enter two numbers and perform all arithmetic
operations.
public class Arithmaticoperations {

public static void main(String[] args) {


int a = 5 ;
int b = 6 ;
int Add,Sub, Div,Mul;
Add= a+b;
Sub=a-b;
Div=a/b;
Mul=a*b;
System.out.println("Addition is:" + Add);
System.out.println("Subtraction is:" + Sub);
System.out.println("Division is:" + Div);
System.out.println("Multiplication is:" + Mul);

Output:

7. Swap values of two integer variables using third variable.


public class SwappingNumber {

public static void main(String[] args) {


int x = 10, y = 20;
int z;
z = x;
x = y;
y = z;

System.out.println("x = " + x);


System.out.println("y = " + y);
}
}

Output:

8. Note - also do same program without using third variable


public class SwapWithoutUsingThirdValue {

public static void main(String[] args) {

int x = 10;
int y = 20;

x = x + y;
y = x - y;
x = x - y;

System.out.println("After swapping: "+ x +" " + y);


}
}

Output:

9. Write a Java program to enter P, T, R and calculate Simple Interest.


public class Simpleinterest {

public static void main (String args[])


{
float Principal, Rate, Time, SimpleInterest;
Principal = 20000;
Rate = 12;
Time = 4;
SimpleInterest = (Principal*Time*Rate)/100;
System.out.println("Simple Interest is: " + SimpleInterest );
}
}
Output:

10.Write a Java program to enter marks of five subjects and calculate total and
Percentage.
public class SubjectTotalandPercentage {
public static void main(String[] args) {

int Maths = 95, Science= 99, History= 78, Arts=55, Geography=45 ;


int MaxMarks = 500;
int Total;

Total = Maths+Science+History+Arts+Geography;

float Percentage= (float)((Total*100)/MaxMarks);

System.out.println("Total of 5 Subject is:" + Total);


System.out.println("Percentage is:" + Percentage);
}}
Output:

11.What will be output of following code?


public class Test {
public static void main(String[] args) {
int a = 5 ;
int b =20 ;
System.out.println(a++ + --b + --b + b/a + --a * b++);
System.out.println("a= " + a + " b= " + b);
}
}

Output:
135
a= 5 b= 19

12.State true or false. Rewrite statement correctly if false.


a. Java program compiles and interprets each time you execute it  True
b. Java is platform independent but JVM is not True
13.WAP to find area and perimeter of rectangle
public class Rectangle{
public static void main(String args[]){

float width= 7;
float height= 8;

float perimeter= 2*(width + height);


float area= width*height;

System.out.println("Perimeter of rectangle is:" + perimeter);


System.out.println("Area of rectangle is:" + area);
}
}

14.Write a Java program to convert days into years, weeks and days.
public class DaysConverter {

public static void main(String[] args) {

int days=799, years, weeks;


years = (days / 365);
weeks = (days % 365) / 7;
days = days - ((years * 365) + (weeks * 7));

System.out.println("Year's = " + years);


System.out.println("Week's = " + weeks);
System.out.println("Day's = " + days);
}
}

You might also like