Comp Assignment 10th
Comp Assignment 10th
Question 1:
Write a program to accept a word from the user and generate the following
import java.util.Scanner;
eg., PROGRAM
PR
PRO
PROG
PROGR
PROGRA
PROGRAM*/
{//start of class
{//start of main()
String s=sc.next();
s=s.toUpperCase();
for(int i=0;i<s.length();i++)
{ //space loop
System.out.print(" ");
//character loop
2
for(int j=0;j<=i;j++)
System.out.print(s.charAt(j));
System.out.println();
}//end of main()
}//end of class
3
Question 2:
Write a program to form a new word by extracting the first letter of each word
/** A Program to accept a Sentence and form a new word by extracting the first
Output: TiM */
{//start of class
{//start of method
System.out.println("Enter a sentence...");
for(int i=0;i<s.length();i++)
{ char ch=s.charAt(i);
if(ch!=32)
str+=ch;
else
newWord=newWord+str.charAt(0); str="";
}//end of method
}//end of class
5
Question 3:
Define a class to accept values in an integer array of size 10. Sort them in an
ascending order using selection sort technique. Display the sorted array.
import java.util.*;
{//start of class
{//start of method
for(int i=0;i<arr.length;i++)
arr[i]=sc.nextInt();
for(int i=0;i<arr.length;i++)
System.out.println(arr[i]);
int min=arr[i];
int pos=i;
6
if(min>arr[j])
min=arr[j]; pos=j;
int t=arr[i];
arr[i]=arr[pos];
arr[pos]=t;
for(int i=0;i<arr.length;i++)
System.out.println(arr[i]);
}//end of method
}//end of class
7
Question 4:
Define a class to accept values into a double dimensional array as below and
check if it is a special array. An array is a special array if the sum of the even
Example:
/** WAP to accept a double dimensional array. Find the sum of even and odd
import java.util.*;
{//start of class
void main()
{//start of method
int m=sc.nextInt();
int n=sc.nextInt();
int sumEven=0;
int sumOdd=0;
for(int i=0;i<m;i++)
{
8
for(int j=0;j<n;j++)
System.out.println("Enter the element at "+ (i+1) + " row and " +(j+1)
+ " column");
arr[i][j]=sc.nextInt();
if(arr[i][j]%2==0)
sumEven+=arr[i][j];
else
sumOdd+=arr[i][j];
}}
System.out.println("\n\nTHE MATRIX");
System.out.println("-----------");
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
System.out.print(arr[i][j]+"\t");
System.out.println();
if(sumEven==sumOdd)
else
}//end of method
}//end of class
9
Question 5:
Define a class to accept and store 10 strings into an array and print the strings
import java.util.*;
/** A program to print the words with even number of characters in an array of
10 Strings(Words)*/
{//start of class
{//start of method
for(int i=0;i<arr.length;i++)
arr[i]=sc.nextLine();
for(int i=0;i<arr.length;i++)
System.out.println(arr[i]);
for(int i=0;i<arr.length;i++)
{
10
if(arr[i].length()%2==0)
System.out.println(arr[i]);
}//end of method
}//end of class
11
Question 6:
State the purpose and return data type of the following String functions: [4]
1. indexOf():- This method gives the first index of the specified character in
the string.
differences.
Question 7:
1. equals() and = =
1) equals() ==
1) It is a method.
1) It is an operator.
2) charAt() substring()
index.
in ascending or descending
order. It is mandatory.
It is not mandatory.
order.
element.
Question 8:
Ans) 14 bytes
Ans) 24 bytes
14
Question 9:
1. "MISSISSIPPI".indexOf('S') +
"MISSISSIPPI".lastIndexOf('I');
Output:- 12
2. "TRANSPARENT".compareTo("TRANSITION");
Output:- 7
3. "JAVAJ2EE".substring(2,5).substring(1).charAt(1);
Output:- J
15
Question 10:
1. int a[10];
Ans) Incorrect
Ans) Correct
Ans) Incorrect
Ans) Incorrect
Ans) Incorrect