Que:1 What is Datatype and how many type of datatypes in java explain in brief?
---> A data type, in programming, is a classification that specifies which type of value a variable has and
what type of mathematical, relational or logical operations can be applied to it without causing an error.
Datatype : Two Types Of Datatype
1)Primitive data types
2)Non-primitive data types
Primitive Data Types:-
A primitive data type specifies the size and type of variable values, and it has no additional methods.
Data Type Size Description
byte 1 byte Stores whole numbers from -128 to 127
short 2 bytes Stores whole numbers from -32,768 to 32,767
int 4 bytes Stores whole numbers from -2,147,483,648 to 2,147,483,647
long 8 bytes Stores whole numbers from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
float 4 bytes Stores fractional numbers. Sufficient for storing 6 to 7 decimal digits
double 8 bytes Stores fractional numbers. Sufficient for storing 15 decimal digits
boolean 1 bit Stores true or false values
char 2 bytes Stores a single character/letter or ASCII values
# Primitive number types are divided into two groups:
1) Integer types stores whole numbers, positive or negative (such as 123 or -456), without decimals.
Valid types are byte, short, int and long. Which type you should use, depends on the numeric value.
2) Floating point types represents numbers with a fractional part, containing one or more decimals.
There are two types: float and double.
#Integer Types
1)Byte:-
The byte data type can store whole numbers from -128 to [Link] can be used instead of int or other integer
types to save memory when you are certain that the value will be within -128 and 127.
2)Short:-
The short data type can store whole numbers from -32768 to 32767.
3)Int:-
The int data type can store whole numbers from -2147483648 to 2147483647. In general, and in our tutorial,
the int data type is the preferred data type when we create variables with a numeric value.
4)Long:-
The long data type can store whole numbers from -9223372036854775808 to 9223372036854775807.
This is used when int is not large enough to store the value. Note that you should end the value with an "L".
5)Float:-
The float data type can store fractional numbers from 3.4e−038 to 3.4e+038. Note that you should end the value wi
th an "f".
6)Double:-
The double data type can store fractional numbers from 1.7e−308 to 1.7e+308. Note that you should end the value
with a "d"
7)Booleans:-
A boolean data type is declared with the boolean keyword and can only take the values true or false.
8)Characters:-
The char data type is used to store a single character. The character must be surrounded by single quotes, like 'A' o
r 'c'.
9)Strings:-
The String data type is used to store a sequence of characters (text). String values must be surrounded by double q
uotes.
Non-Primitive Data Types:-
Non-primitive data types are called reference types because they refer to objects.
# The main difference between primitive and non-primitive data types are:
a) Primitive types are predefined (already defined) in Java. Non-primitive types are created by the programmer and
is not defined by Java (except for String).
b) Non-primitive types can be used to call methods to perform certain operations, while primitive types cannot.
c) A primitive type has always a value, while non-primitive types can be null.
d) A primitive type starts with a lowercase letter, while non-primitive types starts with an uppercase letter.
e) The size of a primitive type depends on the data type, while non-primitive types have all the same size.
Examples of non-primitive types are Strings, Arrays, Classes, Interface, etc.
*********************************************************************************************
****************************************************************************************
Que:2 What will be the output of the following program?
public class JavaDemo1{
public static void main(String[] args)
{
int a=11, b=22, c
c = a + b + a++ + b++ + ++a + ++b;
[Link](“a:- ”+a);
[Link](“b:- ”+b);
[Link](“c:- ”+c);
}
}
Output:-
a:- 13
b:- 24
c:- 103
*********************************************************************************************
****************************************************************************************
Que:3 Write a Program to take a values of length and breadth of a rectangle from user and check if it is square or no
t?
mport [Link].*;
class CheckIf
public static void main(String args[])
Scanner sc = new Scanner([Link]);
[Link]("Enter the length: ");
double l=[Link]();
[Link]("Enter the breadth: ");
double b=[Link]();
if(l==b)
[Link]("Square...");
else
[Link]("Not a Square...");
*********************************************************************************************
****************************************************************************************
Que:4 Write a Program to swap to numbers without using 3rd variable ?
import [Link].*;
class Swap
{
public static void main(String a[])
{
[Link]("Enter the value of x and y");
Scanner sc = new Scanner([Link]);
int x = [Link]();
int y = [Link]();
[Link]("before swapping numbers: "+x +" "+ y);
x = x + y;
y = x - y;
x = x - y;
[Link]("After swapping: "+x +" " + y);
}
}
*********************************************************************************************
****************************************************************************************
Que:5 Write a program to check whether a entered character is lowercase (a to z ) or uppercase ( A to Z ).
import [Link];
public class CheckUpperLower
{
public static void main(String args[])
{
char ch;
Scanner scan=new Scanner([Link]);
[Link]("Enter the character ");
ch=[Link]().charAt(0);
if(ch>='A' && ch<='Z')
{
[Link](ch+" is an upper case letter ");
}
else if(ch>='a' && ch<='z')
{
[Link](ch+" is a lower case letter ");
}
else
{
[Link](ch+" is not a Alphabets ");
}
}
}
*********************************************************************************************
****************************************************************************************
Que:6 Write a Program to accept a number from user check if it is Strong or not?
import [Link].*;
public class Main
{
public static void main(String[] args)
{
int n,i;
int fact,rem;
Scanner sc = new Scanner([Link]);
[Link]("\nEnter the number : ");
n = [Link]();
int sum = 0;
int temp = n;
while(n != 0)
{
i = 1;
fact = 1;
rem = n % 10;
while(i <= rem)
{
fact = fact * i;
i++;
}
sum = sum + fact;
n = n / 10;
}
if(sum == temp)
[Link](temp + " is a strong number\n");
else
[Link](temp + " is not a strong number\n");
[Link]();
}
}
*********************************************************************************************
****************************************************************************************
Que:7
class PrimeNumbers
{
public static void main (String[] args)
{
int i =0;
int num =0;
String primeNumbers = "";
for (i = 1; i <= 100; i++)
{
int counter=0;
for(num =i; num>=1; num--)
{
if(i%num==0)
{
counter = counter + 1;
}
}
if (counter ==2)
{
primeNumbers = primeNumbers + i + " ";
}
}
[Link]("Prime numbers from 1 to 100 are :");
[Link](primeNumbers);
}
}
Que:8 Write a program to print following patterns ?
1)
*
***
*****
*******
*********
import [Link];
public class Pattern1
{
public static void main(String[] args)
{
Scanner scanner = new Scanner([Link]);
[Link]("Enter the number of rows needed to print the pattern ");
int rows = [Link]();
[Link](" Printing the pattern ");
for (int i=1; i<=rows; i++)
{
// Print space in decreasing order
for (int j=rows; j>i; j--)
{
[Link](" ");
}
// Print star in increasing order
for (int k=1; k<=(i * 2) -1; k++)
{
[Link]("*");
}
[Link]();
}
[Link]();
}
}
2)
*********
*******
*****
***
*
import [Link];
public class Pattern2
{
public static void main(String[] args)
{
Scanner scanner = new Scanner([Link]);
[Link]("Enter the number of rows needed to print the pattern ");
int rows = [Link]();
[Link](" Printing the pattern ");
for (int i=rows; i>=1; i--)
{
// Print star in decreasing order
for (int k=1; k<=(i * 2) -1; k++)
{
[Link]("*");
}
[Link]();
// Print space in increasing order
for (int j=rows; j>=i; j--)
{
[Link](" ");
}
}
[Link]();
}
}
3)
1
21
321
4321
import [Link];
public class Pattern3
{
public static void main(String[] args)
{
Scanner scanner = new Scanner([Link]);
[Link]("Enter the number of rows needed to print the pattern ");
int rows = [Link]();
[Link](" Printing the pattern ");
for (int k=1; k<=rows; k++)
{
for (int j=k; j>=1; j--)
{
[Link](j+" ");
}
[Link]();
}
[Link]();
}
}
4}
1
01
101
0101
import [Link];
public class Pattern4
{
public static void main(String[] args)
{
Scanner scanner = new Scanner([Link]);
[Link]("Enter n: ");
int n = [Link]();
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
if ((i + j) % 2 == 0) {
[Link]("1 ");
} else {
[Link]("0 ");
}
}
[Link]();
}
}
}
5)
A
BG
CHM
DINS
EJOTY
FKPUZ_
public class Pattern5
{
public static void main(String[] args)
{
[Link](" Printing the pattern ");
for (int i = 0; i <= 5; i++)
{
int alphabet = 65;
int temp = i;
for (int j = i; j >= 0; j--)
{
[Link]((char) (alphabet + temp) + " ");
temp = temp + 5;
}
[Link]();
}
}
}
6)
F
FE
FED
FEDC
FEDCB
FEDCBA
public class Pattern6
{
public static void main(String[] args)
{
[Link]("** Printing the pattern... **");
for (int i = 5; i >= 0; i--)
{
int alphabet = 65;
for (int j = 5; j >= i; j--)
{
[Link]((char) (alphabet + j) + " ");
}
[Link]();
}
}
}
*********************************************************************************************
****************************************************************************************
Que:9 Can you pass the negative number as an array size? and Why?
---->
No, array size cannot be negative. If we specify array size as negative, there will be no compile time error.
But there will be run time NegativeArraySizeException.
*********************************************************************************************
****************************************************************************************
Que:10 Can you change the size of the array once you define it? OR Can you insert or delete the elements after crea
ting an array? and why?
---->
No. You can’t change the size of the array once you define it. You can not insert or delete the elements
after creating an [Link] you can do is change the value of the elements.
*********************************************************************************************
****************************************************************************************
Que:11 Write a program to sort element of arrays in Descending order?
import [Link];
public class Descending_Order
{
public static void main(String[] args)
{
int n, temp;
Scanner s = new Scanner([Link]);
[Link]("Enter no. of elements you want in array:");
n = [Link]();
int a[] = new int[n];
[Link]("Enter all the elements:");
for (int i = 0; i < n; i++)
{
a[i] = [Link]();
}
for (int i = 0; i < n; i++)
{
for (int j = i + 1; j < n; j++)
{
if (a[i] < a[j])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
[Link]("Descending Order:");
for (int i = 0; i < n - 1; i++)
{
[Link](a[i] + ",");
}
[Link](a[n - 1]);
}
}
*********************************************************************************************
****************************************************************************************
Que:12 Write a program to display sum and average of array elements ?
--->
import [Link];
public class Sum_Average
{
public static void main(String[] args)
{
int n, sum = 0;
float average;
Scanner s = new Scanner([Link]);
[Link]("Enter no. of elements you want in array:");
n = [Link]();
int a[] = new int[n];
[Link]("Enter all the elements:");
for(int i = 0; i < n ; i++)
{
a[i] = [Link]();
sum = sum + a[i];
}
[Link]("Sum:"+sum);
average = (float)sum / n;
[Link]("Average:"+average);
}
}
*********************************************************************************************
****************************************************************************************