You are on page 1of 11

1-Fundamentals of Programming

=============================================================================
1. What is Datatype and how many type of datatypes in java explain in brief?
Ans:-
DATA TYPE
---------
|
----------------------
| |
Primitive Data Non-Primitive Data
Type Type
|
------------------
| |
Boolean Numeric
(True/False) |
(1-byte) --------------
| |
Character Integral
(\u000) |
(2-byte) -------------------
| |
Integer Floating-point
| |
------------------------- ------------
| | | | | |
byte short int long float double
(1-byte)(2-byte)(4-byte)(8-byte)(4-byte) (8-byte)

2. What will be the output of the following program?


package Assignment;
public class Basic {
public static void main(String[] args)
{
int a=11,b=22,c;
c=a+b + a++ + b++ + ++a + b++;
System.out.println("a is :- "+a);
System.out.println("b is :- "+b);
System.out.println("c is :- "+c);
}
}

Ans:-

a is :- 13
b is :- 24
c is :- 102
===========================================================================

3. Write a Program to take a values of length and breadth of a rectangle


from user and check if it is square or not?
Ans:-
Program:-
package Assignment;
import java.util.Scanner;
public class Square {
public static void main(String[] args)
{
Scanner sc=new Scanner (System.in);
System.out.println("Enter The Value of length");
int length=sc.nextInt();
System.out.println("Enter The Value of breadth ");
int breadth =sc.nextInt();
if(length==breadth )
{
System.out.println("It is a Square");
}
else
{
System.out.println("It is not a Square");
}
}
}
output:-
Case 1:-
Enter The Value of length
5
Enter The Value of breadth
5
It is a Square
case 2:-
Enter The Value of length
4
Enter The Value of breadth
6
It is not a Square
===========================================================================
4. Write a Program to swap to numbers without using 3rd variable ?
Ans:-
Program:-
package Assignment;
import java.util.Scanner;
public class Swap
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter value of x");
int x=sc.nextInt();
System.out.println("Enter value of y");
int y=sc.nextInt();
System.out.println("Before swapping x is "+x+" y is "+y);
x=x+y;
y=x-y;
x=x-y;
System.out.println("After Swapping value of x is "+x+"y is "+y);
}
}
Output:-
Enter value of x
55
Enter value of y
99
Before swapping x is 55 y is 99
After Swapping value of x is 99y is 55
===========================================================================
5.Write a program to check whether a entered
character is lowercase (a to z ) or uppercase ( A to Z ).
Ans :-
Program:-
package Assignment;
import java.util.Scanner;
public class Inputchar
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter The Character");
char ch=sc.next().charAt(0);
if(ch>=65 && ch<=90)
{
System.out.println("This is Upper case Character");
}
else
{
System.out.println("This is Lower Case Character");
}
}
}

Output:-
Case 1:-
Enter The Character
G
This is Upper case Character
Case 2:-
Enter The Character
h
This is Lower Case Character

===========================================================================
6. Write a Program to accept a number from user check if it is Strong or not?
e.g int num = 145
int sum = 1! + 4! + 5! ;
sum = 145
Ans:-
Program:-
package Assignment;
import java.util.Scanner;
public class AmstrongNumber
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter Number");
int num=sc.nextInt();
int temp=num;
int reminder;
int sum=0;
while(num<0 || num>0)
{
reminder=num%10;
num=num/10;
sum=sum+(reminder*reminder*reminder);
System.out.println("reminder is "+reminder);
}
System.out.println("Sum is "+sum);
if(temp==sum)
{
System.out.println("This is Amstrong Number");
}
else
{
System.out.println("This Not a Amstrong Number");
}
}
}
Output:-
case 1:-
Enter Number
153
reminder is 3
reminder is 5
reminder is 1
Sum is 153
This is Amstrong Number
Case 2:-
Enter Number
142
reminder is 2
reminder is 4
reminder is 1
Sum is 73
This Not a Amstrong Number

===========================================================================
7. Write a Program to print prime number between 1 to 100.
Ans:-
Program:-
package Assignment;
public class PrimeNumber
{
public static void main(String[] args)
{
int count;
for(int i=1;i<=100;i++)
{
count=0;
for(int j=2;j<=i/2;j++)
{
if(i%j==0)
{
count++;
}
}
if(count==0 && i!=0)
{
System.out.print(" "+i);
}
}
}
}
Output:-
1 2 3 5 7 11 13 17 19 23 29 31 37
41 43 47 53 59 61 67 71 73 79 83 89 97

===========================================================================
8. Write a program to print following patterns ?
Anw:-
a. Program:-
package Assignment;
public class Pyramid
{
public static void main(String[] args)
{
for(int i=0;i<=5;i++)
{
for(int sp=0;sp<=5-i;sp++)
{
System.out.print(" ");
}
for(int j=1;j<=((2*i)-1);j++)
{
System.out.print("*");
}
System.out.println();
}
}
}
Output:-

*
***
*****
*******
*********

===========================================================================
b.Program:-
package Assignment;
public class Pyramid2
{
public static void main(String[] args)
{
for(int i=5;i>=1;i--)
{
for(int sp=0;sp<=5-i;sp++)
{
System.out.print(" ");
}
for(int j=1;j<=((2*i)-1);j++)
{
System.out.print("*");
}
System.out.println();
}
}
}
Output:-
*********
*******
*****
***
*
===========================================================================
c.Program:-
package Assignment;
public class Pattern
{
public static void main(String[] args)
{
for(int i=0;i<=5;i++)
{
for(int j=i;j>=1;j--)
{
System.out.print(j);
}
System.out.println();
}
}
}
Output:-
1
21
321
4321
54321
===========================================================================
d.Program:-
package Assignment;
public class Pattern1or0 {
public static void main(String[] args) {
for (int i = 0; i <= 3; i++)
{
int temp = i;
for (int j = i; j >= 0; j--)
{
temp = temp + 5;
if(temp%2==0)
{
System.out.print(" 0 ");
}
else
{
System.out.print(" 1 ");
}
}
System.out.println();
}
}
}
Output:-
1
0 1
1 0 1
0 1 0 1
===========================================================================
e.Program:-
package Assignment;
public class Charpattern
{
public static void main(String[] args)
{
for (int i = 0; i <= 5; i++)
{
int alphabet = 65;
int temp = i;
for (int j = i; j >= 0; j--)
{
System.out.print((char) (alphabet + temp) + " ");
temp = temp + 5;
}
System.out.println();
}
}
}
Output:-
A
B G
C H M
D I N S
E J O T Y
F K P U Z _
===========================================================================

f.Program:-
package Assignment;
public class Reverseprint {
public static void main(String[] args) {
for(int i=70;i>=65;i--)
{
for(int j=70;j>=i;j--)
{
System.out.print((char)j);
}
System.out.println();
}
}
}
Output:-
F
FE
FED
FEDC
FEDCB
FEDCBA
===========================================================================

9. Can you pass the negative number as an array size? and Why?
Ans:-
in Eclipse i write a line int arr[]=new int[-4]; in main method then i gives
Negative Array Size Exception that means we cannot pass negative array as an size
becouse the size of an array represents the number of elements in it,
–ve number of elements in an array makes no sense.
still if we do so, the program gets compiled without issues but, while executing
it generates a runtime exception of type NegativeArraySizeException

===========================================================================

10. Can you change the size of the array once you define it? OR
Can you insert or delete the elements after creating an array? and why?
Ans:-
No. we can’t change the size of the array once you define it.
if can not insert or delete the elements after creating an array.
Only you can do is change the value of the elements.
If you create an array by initializing its values directly, the
size will be the number of elements in it.
Thus the size of the array is determined at the time of its creation or,
initialization once it is done you cannot change the size of the array.
===========================================================================

11.Write a program to sort element of arrays in Descending order?


Ans:-
Program:-
package Assignment;
import java.util.Scanner;
public class Array {
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter Array Size");
int size=sc.nextInt();
int arr[]=new int[size];
System.out.println("Enter The Elements");
for(int ele=0;ele<arr.length;ele++)
{
arr[ele]=sc.nextInt();
}
for(int i=0;i<arr.length;i++)
{
for(int j=i+1;j<arr.length;j++)
{
if(arr[i]<=arr[j])
{
int temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
System.out.println("Sorting Elements in Descending Order");
for(int ele=0;ele<arr.length;ele++)
{
System.out.println(arr[ele]);
}
}
}
Output:-
Enter Array Size
5
Enter The Elements
4
6
8
2
9
Sorting Elements in Descending Order
9
8
6
4
2
===========================================================================

12.Write a program to display sum and average of array elements ?


Ans:-
Program:-
package Assignment;
import java.util.Scanner;
public class ArrayAvrage
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter Array Size");
int size=sc.nextInt();
int arr[]=new int[size];
float sum=0;
System.out.println("Enter The Elements");
for(int ele=0;ele<arr.length;ele++)
{
arr[ele]=sc.nextInt();
}
System.out.println("Given Elements");
for(int ele=0;ele<arr.length;ele++)
{
System.out.println(arr[ele]);
sum=sum+arr[ele];
}
System.out.println("Sum of Array Elements is:- "+sum);
float avrage=sum/size;
System.out.println("Avrage of All Elements is:- "+avrage);
}
}
Output:-
Enter Array Size
5
Enter The Elements
4
5
6
7
8
Given Elements
4
5
6
7
8
Sum of Array Elements is:- 30.0
Avrage of All Elements is:- 6.0

You might also like