100% found this document useful (1 vote)
215 views

Assessment Milestone

The document contains 13 questions related to Java fundamentals and programming concepts like: 1. Accepting command line arguments and performing operations like string concatenation, addition etc. 2. Using conditional statements like if-else, switch case to check conditions. 3. Using loops like for loop to iterate and print patterns. 4. Checking for prime numbers and even/odd numbers. 5. The questions cover basic to intermediate Java concepts.

Uploaded by

PONNAMANENI INDU
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
215 views

Assessment Milestone

The document contains 13 questions related to Java fundamentals and programming concepts like: 1. Accepting command line arguments and performing operations like string concatenation, addition etc. 2. Using conditional statements like if-else, switch case to check conditions. 3. Using loops like for loop to iterate and print patterns. 4. Checking for prime numbers and even/odd numbers. 5. The questions cover basic to intermediate Java concepts.

Uploaded by

PONNAMANENI INDU
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 37

Assessment Milestone-I

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

public static void main (String[] args)

System.out.print(args[0]+"Technologies"+args[1]);

E:\>Javac Sample.Java

E:\>java Sample Wipro Bangalore

Output: Wipro Technologies Bangalore

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

public static void main (String[] args)

System.out.print("Welcome"+args[0]);

E:\>Java Sample.java

E:\>Java Sample John

Output: Welcome John


Q3.Write a program that accepts two integers as a command line arguments and print the sum of
two numbers.

Code:

import java.util.*;

class Sample

public static void main (String[] args)

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

2.Flow Control Statements:-


Q1A. Write a program to check if the given number is positive, negative or zero.

Code:

import java.util.*;

class Sample

public static void main (String[] args)

Scanner sc=new Scanner(System.in);

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

Output: Positive Integer

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

public static void main (String[] args)

Scanner sc=new Scanner(System.in);

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.

Q2. Write a program to check if a given number is even or odd.

Code:

import java.util.*;
class Sample

public static void main (String[] args)

Scanner sc=new Scanner(System.in);

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

public static void main (String[] args)

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

public static void main (String[] args)

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

public static void main (String[] args)

{
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

public static void main (String[] args)

int age=Integer.parseInt(args[0]);

String str=args[1];

if(str.equals("Female") && (age>1 && age<58))

System.out.print("8.2%");

if(str.equals("Female")&&(age>59 && age<100))

System.out.print("9.2%");
if(str.equals("Male")&&(age>1 && age<58))

System.out.print("8.4%");

if(str.equals("Male")&&(age>59 && age<100))

System.out.print("10.5%");

}}

E:\>Javac Sample.java

E:\>java Sample 60 Male

Output: 10.5%

Q7. Initialize a character variable with an alphabet in a program

If the character value is in lowercase , the output should be displayed in uppercase vice versa.

Code:

import java.util.*;

class Sample

public static void main (String[] args)

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

public static void main (String[] args)

Scanner sc=new Scanner(System.in);

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

public static void main (String[] args)

Scanner sc=new Scanner(System.in);

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:

System.out.println ("You have entered an invalid number");

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

public static void main (String[] args)

Scanner sc=new Scanner(System.in);

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

public static void main (String[] args)

Scanner sc=new Scanner(System.in);

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

Q12. Write a program to check if a given number is prime or not.

Code:

import java.util.*;

class Sample

public static void main (String[] args)

Scanner sc=new Scanner(System.in);

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

Q13. Write a program to print prime numbers between 10 and 99.

Code:

import java.util.*;

class Sample

public static void main (String[] args)

Scanner sc=new Scanner(System.in);

int i =0;

int num =0;

String UniqNoDemo = "";

for (i = 10; i <= 99; i++)

int myFlag=0;

for(num =i; num>=1; num--)

if(i%num==0)

myFlag = myFlag + 1;
}

if (myFlag ==2)

UniqNoDemo = UniqNoDemo + i + " ";

System.out.println("Prime numbers from 1 to 100 are :");

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

public static void main (String[] args)

Scanner sc=new Scanner(System.in);

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

Q15. Write a program to print * in Floyds format.

Code:

import java.util.*;

class Sample

public static void main (String[] args)

Scanner sc=new Scanner(System.in);

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:

**
***

Q16. Write a program to reverse a given number and print.

Code:

import java.util.*;

class Sample

public static void main (String[] args)

Scanner sc=new Scanner(System.in);

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

Q17. Write a program to find if the number is palindrome or not.

Code:

import java.util.*;

class Sample

public static void main (String[] args)

{
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

Output: not palindrome

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

public static void main (String[] args)

Scanner sc=new Scanner(System.in);

int n=sc.nextInt();

int arr[]=new int[n];


for(int i=0;i<n;i++)

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

public static void main (String[] args)

Scanner sc=new Scanner(System.in);

int arr[]=new int[10];

System.out.println("Enter elements in array");

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

public static void main (String[] args)

Scanner sc=new Scanner(System.in);


int n=sc.nextInt();

int k=sc.nextInt();

int arr[]=new int[n];

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

public static void main (String[] args)

Scanner sc=new Scanner(System.in);


int n=sc.nextInt();

int arr[]=new int[n];

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

public static void main (String[] args)

Scanner sc=new Scanner(System.in);

int n=sc.nextInt();

int arr[]=new int[n];

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

public static void main (String[] args)

Scanner sc=new Scanner(System.in);

int n=sc.nextInt();

int arr[]=new int[n];

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

public static void main (String[] args)

Scanner sc=new Scanner(System.in);

int n=sc.nextInt();

int arr[]=new int[n];

for(int i=0;i<n;i++)

arr[i]=sc.nextInt();

int[] temp = new int[n];

int j = 0;

for (int i = 0; i < n - 1; i++) {

if (arr[i] != arr[i + 1]) {

temp[j++] = arr[i];

temp[j++] = arr[n - 1];

for (int i = 0; i < j; 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

public static void main (String[] args)

Scanner sc=new Scanner(System.in);

int n=sc.nextInt();

int arr[]=new int[n];

for(int i=0;i<n;i++)

arr[i]=sc.nextInt();

int sum = 0;

boolean add = true;

int a=6;

int b=7;

for (int i=0; i<arr.length; i++) {

if (arr[i] != a && add == true)

sum = sum + arr[i];


else if (arr[i] == a)

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

public static void main (String[] args)

Scanner sc=new Scanner(System.in);

int n=sc.nextInt();

int arr[]=new int[n];

for(int i=0;i<n;i++)

arr[i]=sc.nextInt();

int l=0;

int res[]=new int[n];

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

public static void main (String[] args)

Scanner sc=new Scanner(System.in);

int n=sc.nextInt();

int arr[]=new int[n];

for(int i=0;i<n;i++)

arr[i]=sc.nextInt();
}

int odd[]=new int[10];

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

Q11. Given an array of type int, print true if every element is 1 or 4.

Code:

import java.util.*;

class Sample

{
public static void main (String[] args)

Scanner sc=new Scanner(System.in);

int n=sc.nextInt();

int arr[]=new int[n];

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
{

public static void main (String[] args)

Scanner sc=new Scanner(System.in);

int n=sc.nextInt();

int arr[]=new int[3];

int arr1[]=new int[3];

for(int i=0;i<3;i++)

arr[i]=sc.nextInt();

for(int i=0;i<3;i++)

arr1[i]=sc.nextInt();

int res[]=new int[2];

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
{

public static void main (String[] args)

int arr[][]=new int[2][2];

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

public static void main (String[] args)

{
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:

public class Box{

int width,height,depth;

Box(int w,int h,int d)


{

width=w;

height=h;

depth=d;

int Volume()

int v;

v=width*height*depth;

return v;

public static void main(String args[])

Box obj=new Box(2,4,6);

System.out.print(obj.Volume());

E:\>javac Box.java

E:\>java Box

Output:48

Q2. Create a new class calculator with the following methods.

1.A static method called powerInt(int num1,int num2)

This method should return num1 to the power num2.

2.A static method called powerDouble(double num1,double num2)

This method should return num1 to the power num2.

3.Invoke both the method and test the functionalities.

Code:

public class Calculator{

static double powerInt(int num1,int num2)

return Math.pow(num1,num2);
}

static double powerDouble(double num1,int num2)

return Math.pow(num1,num2);

public static void main(String args[])

Calculator obj=new Calculator();

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.

Member Variables : name(String) , email(String), and gender(char) parameterized constructor : To


initialize the variables.

Create a class Book with the following information.

Member variables: name(String), author , price(double), and qtyInStock(int).

Getters and setters for all the member variables.

In the main method, create object for the Book and print all the details of the book including author
also.

Code:

class Author {

private String name;

private String email;

private char gender;


public Author(String name, String email, char gender) {

super();

this.name = name;

this.email = email;

this.gender = gender;

public String getName() {

return name;

public String getEmail() {

return email;

public char getGender() {

return gender;

@Override

public String toString() {

return "Author [name=" + name + ", email=" + email + ", gender=" + gender + "]";

class Book {

private String name;

private Author author;

private double price;

private int qtyInStock;

public Book(String name, Author author, double price, int qtyInStock) {

super();

this.name = name;

this.author = author;

this.price = price;

this.qtyInStock = qtyInStock;
}

public double getPrice() {

return price;

public void setPrice(double price) {

this.price = price;

public int getQtyInStock() {

return qtyInStock;

public void setQtyInStock(int qtyInStock) {

this.qtyInStock = qtyInStock;

public String getName() {

return name;

public Author getAuthor() {

return author;}

@Override

public String toString() {

return "Book [name=" + name + ", author=" + author + ", price=" + price + ", qtyInStock=" +
qtyInStock + "]";

public class Assignment1 {

public static void main(String[] args) {

Author author = new Author("Arpan Das", "arp14@yahoo.com", 'M');

Book book = new Book("Java Fundamentals", author, 199.0, 500);

System.out.println(book);

}
E:\>javac Assignment1.java

E:\>java Assignment

Output:

Book [ name= Java Fundamentals , author=Author[name =Arpan Das, email=arp14@yaho0.com,


gender=M], price=199.0, qtyInStocks=500]

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy