Assessment Milestone
Assessment Milestone
Java Fundamentals:
1.Language Basics:-
Q1.Write a program that accepts two strings as command line arguments and generate output in the
required format example Wipro technologies Bangalore.
Code:
class Sample
System.out.print(args[0]+"Technologies"+args[1]);
E:\>Javac Sample.Java
Q2. Write a program to accept a string as a command line argument and print a welcome message
such as welcome John.
Code:
import java.util.*;
class Sample
System.out.print("Welcome"+args[0]);
E:\>Java Sample.java
Code:
import java.util.*;
class Sample
int x=Integer.parseInt(args[0]);
int y=Integer.parseInt(args[0]);
System.out.print(x+y);
E:\>Javac Sample.java
E:\>Java Sample 10 20
Output: 30
Code:
import java.util.*;
class Sample
int n=sc.nextInt();
if(n>0)
System.out.println("Positive Integer");
else if(n<0)
System.out.println("Negative Integer");
else
System.out.println("Zero");
E:\>Javac Sample.java
E:\>Java Sample
10
Q1B. Given two non-negative values, print true if the they have the same last digit , such as with 27
and 57.
Code:
import java.util.*;
class Sample
int n=sc.nextInt();
int m=sc.nextInt();
if(n%10==m%10)
System.out.print("true");
else
System.out.print("false");
E:\>Javac Sample.Java
E:\>Java Sample
27 57
Output: true.
Code:
import java.util.*;
class Sample
int n=sc.nextInt();
if(n%2==0)
System.out.print("even");
else
System.out.print("odd");
E:\>Javac Sample.java
E:\>Java Sample
10
Output: even
Q3. Write a program to check if the program has received command line argument or not, If the
program has not received arguments then print “No Values” else print all the values in a single line
separated by , comma.
Code:
import java.util.*;
class Sample
if(args.length==0)
System.out.print("No Values");
else
System.out.print(args[0]+","+args[1]);
E:\>Javac Sample.java
E:\>java Sample
Output: No Values
Q4.Intialize two character variables in a program and display the characters in alphabetical order.
Code:
import java.util.*;
class Sample
char x='s';
char y='e';
if(x>y)
System.out.print(y+" "+x);
else
System.out.print(x+" "+y);
E:\>Javac Sample.java
E:\>java Sample
Output: e s
Q5. Initialize a character variable in a program and print “Alphabet” if the initial value is alphabet,
Print “Digit” if the initialize value is digit , print “Special Character” if the initialized value is a special
character.
Code:
import java.util.*;
class Sample
{
char x='s';
if(Character.isAlphabetic(x))
System.out.print("Alphabet");
else if(Character.isDigit(x))
System.out.print("Digit");
else
System.out.print("Special Character");
E:\>java Sample
Output: Alphabet
Q6.Write a program to accept gender (“male or Female) and age from command line arguments and
print the percentage of interest based on the given condition.
If the gender is ‘Female’ and age between 1 and 58 , the percentage of interest is 8.2%
If the gender is ‘Female’ and age between 59 and 100 ,the percentage of interest is 9.2%
If the gender is ‘Male’ and age between 1 and 58 ,the percentage of interest is 8.4%
If the gender is ‘Male’ and age between 59 and 100 ,the percentage of interest is 10.5%
Code:
import java.util.*;
class Sample
int age=Integer.parseInt(args[0]);
String str=args[1];
System.out.print("8.2%");
System.out.print("9.2%");
if(str.equals("Male")&&(age>1 && age<58))
System.out.print("8.4%");
System.out.print("10.5%");
}}
E:\>Javac Sample.java
Output: 10.5%
If the character value is in lowercase , the output should be displayed in uppercase vice versa.
Code:
import java.util.*;
class Sample
char str='x';
if(Character. isUpperCase(str))
System.out.print(Character. toLowerCase(str));
else
System.out.print(Character. toUpperCase(str));
E:\>Javac Sample.java
E:\>java Sample
Output: X
Q8. Write a program to receive a color code from the user (an Alphabet). The program should then
print the color name , based on the color code given. The following are the color codes and their
corresponding color names. R-red, G-green, O-orange, B-blue, Y-yellow, W-white. If the color code
provided by the user is not valid then print “Invalid Code”.
Code:
import java.util.*;
class Sample
char ch=sc.next().charAt(0);
if(ch=='Y')
System.out.print("Yellow");
else if(ch=='B')
System.out.print("Blue");
else if(ch=='R')
System.out.print("Red");
else if(ch=='O')
System.out.print("Orange");
else if(ch=='G')
System.out.print("Green");
else if(ch=='W')
System.out.print("White");
else
System.out.print("Invalid code");
E:\>Javac Sample.java
E:\>java Sample
Output: Yellow
Q9. Write a program to receive a number and print the corresponding month name.
Code:
import java.util.*;
class Sample
int num=sc.nextInt();
switch (num)
case 1:
System.out.println ("January");
break;
case 2:
System.out.println ("February");
break;
case 3:
System.out.println ("March");
break;
case 4:
System.out.println ("April");
break;
case 5:
System.out.println ("May");
break;
case 6:
System.out.println("June");
break;
case 7:
System.out.println ("July");
break;
case 8:
System.out.println ("August");
break;
case 9:
System.out.println ("September");
break;
case 10:
System.out.println ("October");
break;
case 11:
System.out.println ("November");
break;
case 12:
System.out.println ("December");
break;
default:
E:\>Javac Sample.java
E:\>java Sample
10
Output: October
Q10. Write a program to print numbers from 1 to 10 in a single row with one tab space.
Code:
import java.util.*;
class Sample
int num=sc.nextInt();
for(int i=1;i<=num;i++)
System.out.print(+i+" ");
E:\>javac Sample.java
E:\>java Sample
10
Output: 1 2 3 4 5 6 7 8 9 10
Q11. Write a program to print even numbers between 23 and 57 . Each number should be printed in
a separate row.
Code:
import java.util.*;
class Sample
for(int i=23;i<57;i++)
if(i%2==0)
System.out.println(+i);
E:\>javac Sample.java
E:\>java Sample
Output:
24
26
28
30
32
34
36
38
40
42
44
46
48
50
52
54
56
Code:
import java.util.*;
class Sample
int num=sc.nextInt();
int flag=0;
for(int i=2;i<=num/2;i++)
if(num%i==0)
flag=1;
break;
}
if(flag==0)
System.out.print("Prime");
else
System.out.print("Not Prime");
E:\>javac Sample.java
E:\>java Sample
Output: Prime
Code:
import java.util.*;
class Sample
int i =0;
int myFlag=0;
if(i%num==0)
myFlag = myFlag + 1;
}
if (myFlag ==2)
System.out.println(UniqNoDemo);
E:\>javac Sample.java
E:\>java Sample
Output: 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
Q14. Write a program to print the sum of all digits in a given number.
Code:
import java.util.*;
class Sample
int n=sc.nextInt();
int sum=0;
while(n!=0)
int res=n%10;
sum+=res;
n/=10;
System.out.print(+sum); }}
E:\>javac Sample.java
E:\>java Sample
1234
Output: 10
Code:
import java.util.*;
class Sample
int n=sc.nextInt();
int i=1;
while(i<=n)
for(int j=1;j<=i;j++)
System.out.print("*"+" ");
i++;
System.out.println();
E:\>javac Sample.java
E:\>java Sample
Output:
**
***
Code:
import java.util.*;
class Sample
int n=sc.nextInt();
int sum=0;
while(n!=0)
int res=n%10;
sum=sum*10+res;
n/=10;
System.out.print(+sum);
E:\>javac Sample.java
E:\>java Sample
1234
Output: 4321
Code:
import java.util.*;
class Sample
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int x=n;
int sum=0;
while(n!=0)
int res=n%10;
sum=sum*10+res;
n/=10;
if(x==sum)
System.out.print("palindrome");
else
System.out.print("not palindrome");
E:\>javac Sample.java
E:\>java Sample
1234
3.Arrays:-
Q1. Write a program to initialize an array and print the sum and average of the array.
Code:
import java.util.*;
class Sample
int n=sc.nextInt();
arr[i]=sc.nextInt();
int sum=0;
for(int i=0;i<n;i++)
sum+=arr[i];
System.out.println(+sum);
System.out.print(+sum/n);
E:\>javac Sample.java
E:\>java Sample
1234
Output:
10
Q2. Write a program to initialize an array and print the maximum and minimum value of the array.
Code:
import java.util.*;
class Sample
int min=Integer.MAX_VALUE;
int max=Integer.MIN_VALUE;
for(int i=0;i<=9;i++)
arr[i]=sc.nextInt();
if(arr[i]<min)
min=arr[i];
if(arr[i]>max)
max=arr[i];
System.out.println(+max);
System.out.println(+min);
E:\>javac Sample.java
E:\>java Sample
1 2 3 4 5 6 7 8 9 10
Output: 10
Q3. Write a program to initialize an integer array with values and check if a given number is present
in the array or not . If the number is not found print -1, else it will print the index of the given
number in that array.
Code:
import java.util.*;
class Sample
int k=sc.nextInt();
for(int i=0;i<n;i++)
arr[i]=sc.nextInt();
int res=-1;
for(int i=0;i<n;i++)
if(arr[i]==k)
res=i;
System.out.print(+res);
E:\>javac Sample.java
E:\>java Sample
1234
Output: 2
Q4. Initialize an integer array with ascii values and print the corresponding character values in a
single row.
Code:
import java.util.*;
class Sample
for(int i=0;i<n;i++)
arr[i]=sc.nextInt();
for(int i=0;i<n;i++)
System.out.print((char)(arr[i])+" ");
E:\>javac Sample.java
E:\>java Sample
65 66 67
Output: A B C
Q5. Write a program to find the largest 2 numbers and the smallest 2 numbers in the given array.
Code:
import java.util.*;
class Sample
int n=sc.nextInt();
for(int i=0;i<n;i++)
arr[i]=sc.nextInt();
}
Arrays.sort(arr);
System.out.println(+arr[0]+" "+arr[1]);
System.out.println(+arr[n-1]+" "+arr[n-2]);
E:\>javac Sample.java
E:\>java Sample
12345
Output: 1 2
54
Q6. Write a program to initialize an array and print them in a sorted order.
Code:
import java.util.*;
class Sample
int n=sc.nextInt();
for(int i=0;i<n;i++)
arr[i]=sc.nextInt();
Arrays.sort(arr);
for(int i=0;i<n;i++)
System.out.print(+arr[i]+" ");
}
}
E:\>javac Sample.java
E:\>java Sample
43251
Output: 1 2 3 4 5
Q7. Write a program to remove the duplicate elements in an array and print the same.
Code:
import java.util.*;
class Sample
int n=sc.nextInt();
for(int i=0;i<n;i++)
arr[i]=sc.nextInt();
int j = 0;
temp[j++] = arr[i];
System.out.print(+temp[i]+" ");
}
E:\>javac Sample.java
E:\>java Sample
11245
Output: 1 2 4 5
Q8. Write a program to print the sum of the elements of an array following the given below
condition.
If the array has 6 and 7 in succeeding orders, ignore the numbers between 6 and 7 and consider the
other numbers for calculation of sum.
Code:
import java.util.*;
class Sample
int n=sc.nextInt();
for(int i=0;i<n;i++)
arr[i]=sc.nextInt();
int sum = 0;
int a=6;
int b=7;
add = false;
else if (arr[i] == b)
add = true;
System.out.println(sum);
E:\>javac Sample.java
E:\>java Sample
10 3 6 1 2 7 9
Output: 22
Q9. Print a version of the given array where all the ten’s have been removed . The remaining
elements should shift left towards the start of the array as needed, and the empty spaces at the end
of the array should be 0.
Code:
import java.util.*;
class Sample
int n=sc.nextInt();
for(int i=0;i<n;i++)
arr[i]=sc.nextInt();
int l=0;
for(int i=0;i<n;i++)
{
if(arr[i]!=10)
res[l++]=arr[i];
for(int i=l;i<n;i++)
res[i]=0;
for(int i=0;i<res.length;i++)
System.out.print(+res[i]+" ");
E:\>javac Sample.java
E:\>java Sample
10 1 10 2
Output: 1 2 0 0
Q10.Print an array that contains the exact same numbers as given in the array, by rearranged so that
all the even numbers come before all the odd numbers. Other than that, the numbers can be in any
order.
Code:
import java.util.*;
class Sample
int n=sc.nextInt();
for(int i=0;i<n;i++)
arr[i]=sc.nextInt();
}
int l=0,r=0;
for(int i=0;i<n;i++)
if(arr[i]%2==0)
arr[l++]=arr[i];
else
odd[r++]=arr[i];
int s=0;
for(int i=l;i<n;i++)
arr[i]=odd[s++];
for(int i=0;i<n;i++)
System.out.print(+arr[i]+" ");
E:\>javac Sample.java
E:\>java Sample
0101001
Output: 0 0 0 0 1 1 1
Code:
import java.util.*;
class Sample
{
public static void main (String[] args)
int n=sc.nextInt();
for(int i=0;i<n;i++)
arr[i]=sc.nextInt();
int c=0;
for(int i=0;i<n;i++)
if(arr[i]==1 || arr[i]==4)
c++;
if(c==n)
System.out.print("true");
else
System.out.print("false");
E:\>javac Sample.java
E:\>java Sample
14141
Output: true
Q12. Given 2 int arrays , a and b , each length 3 , from a new array of length 2, containing their
middle elements .
Code:
import java.util.*;
class Sample
{
int n=sc.nextInt();
for(int i=0;i<3;i++)
arr[i]=sc.nextInt();
for(int i=0;i<3;i++)
arr1[i]=sc.nextInt();
res[0]=arr[1];
res[1]=arr1[1];
System.out.print(+res[0]+" "+res[1]);
E:\>javac Sample.java
E:\>java Sample
123
456
Output: 2 5
Q13. Write a program to reverse the elements of a given 2*2 array .Four integer numbers needs to
be passed as command line arguments.
Code:
import java.util.*;
class Sample
{
int k=0;
for(int i=0;i<2;i++)
for(int j=0;j<2;j++)
arr[i][j]=Integer.parseInt(args[k++]);
for(int i=1;i>=0;i--)
for(int j=1;j>=0;j--)
System.out.print(arr[i][j]+" ");
System.out.println();
E:\>javac Sample.java
E:\>java Sample 1 2 3 4
Output: 4 3
21
Q14. Write a program to find the biggest number in a 3*3 array. The program is supposed to receive
9 integer number as command line arguments.
Code:
import java.util.*;
class Sample
{
int arr[][]=new int[3][3];
int k=0;
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
arr[i][j]=Integer.parseInt(args[k++]);
int max=0;
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
if(arr[i][j]>max)
max=arr[i][j];
System.out.print(+max);
E:\>javac Sample.java
E:\>java Sample 1 2 3 4 5 6 7 8 9
Output: 9
Oops / Inheritance:
1.Classes and Objects:
Q1.Create a class Box that uses a parameterized constructor to initialize the dimensions of a box.
The dimensions of the Box are width, height, depth .The class should have a method that return the
volume of the box. Create an object of box class and test the functionalities.
Code:
int width,height,depth;
width=w;
height=h;
depth=d;
int Volume()
int v;
v=width*height*depth;
return v;
System.out.print(obj.Volume());
E:\>javac Box.java
E:\>java Box
Output:48
Code:
return Math.pow(num1,num2);
}
return Math.pow(num1,num2);
System.out.print(obj.powerInt(2,3));
System.out.println();
System.out.print(obj.powerDouble(85.0,2);
E:\>javac Calculator.java
E:\>java Calculator
Output: 8.0
7225.0
2.Encapsulation/Abstraction:
Q1. Create a class Author with the following information.
In the main method, create object for the Book and print all the details of the book including author
also.
Code:
class Author {
super();
this.name = name;
this.email = email;
this.gender = gender;
return name;
return email;
return gender;
@Override
return "Author [name=" + name + ", email=" + email + ", gender=" + gender + "]";
class Book {
super();
this.name = name;
this.author = author;
this.price = price;
this.qtyInStock = qtyInStock;
}
return price;
this.price = price;
return qtyInStock;
this.qtyInStock = qtyInStock;
return name;
return author;}
@Override
return "Book [name=" + name + ", author=" + author + ", price=" + price + ", qtyInStock=" +
qtyInStock + "]";
System.out.println(book);
}
E:\>javac Assignment1.java
E:\>java Assignment
Output: