Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 19

Name :- Presha Raval

Class/ Div:- X-E


Rollno. 11
1.

Question 1:

Write a program to accept a word from the user and generate the following

pattern using the given word. (Use String functions).

import java.util.Scanner;

/** A program to accept a word and generate the following pattern

eg., PROGRAM

PR

PRO

PROG

PROGR

PROGRA

PROGRAM*/

public class Pattern

{//start of class

public void main()

{//start of main()

Scanner sc=new Scanner(System.in);

System.out.println("Enter the word");

String s=sc.next();

s=s.toUpperCase();

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

{ //space loop

for(int b=i; b<s.length();sp++)

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

in the input sentence.

/** A Program to accept a Sentence and form a new word by extracting the first

letter of each word.

Input: Today is Monday?

Output: TiM */

public class FirstLetter

{//start of class

public void main()

{//start of method

Scanner sc=new Scanner(System.in);

System.out.println("Enter a sentence...");

String s=sc.nextLine(); s=s.trim()+' ';

String str= “” ; String newWord= “”;

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="";

System.out.println("The new word is ..."+newWord);


4

}//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.*;

/** A Program to accept 10 integers numbers in an array and arrange them in

ascending order using selection sort */

public class SelecSort

{//start of class

public static void main()

{//start of method

Scanner sc=new Scanner(System.in);

int arr[]=new int[10];

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

System.out.println("Enter the element at position..."+(i+1));

arr[i]=sc.nextInt();

System.out.println("\nThe original array is...");

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

System.out.println(arr[i]);

//Exchange Selection Sort in ascending order

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

int min=arr[i];

int pos=i;
6

for(int j=i+1; j<arr.length;j++)

if(min>arr[j])

min=arr[j]; pos=j;

int t=arr[i];

arr[i]=arr[pos];

arr[pos]=t;

System.out.println("\nThe Array after sorting...");

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

elements = sum of the odd elements.

Example:

A[][]={{ 4 ,5, 6}, { 5 ,3, 2}, { 4, 2, 5}};

Sum of even elements = 4+6+2+4+2 =18

Sum of odd elements = 5+5+3+5 = 18

/** WAP to accept a double dimensional array. Find the sum of even and odd

elements. if they are equal, the array is special */

import java.util.*;

public class SpecialMatrix

{//start of class

void main()

{//start of method

Scanner sc=new Scanner(System.in);

System.out.println("Enter the number of rows and Columns");

int m=sc.nextInt();

int n=sc.nextInt();

int arr[][]=new int[m][n];

int sumEven=0;

int sumOdd=0;

System.out.println("Enter " + m*n + " numbers in the matrix");

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)

System.out.println("The matrix is a special matrix ");

else

System.out.println("The matrix is not a special matrix ");

}//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

with even number of characters.

import java.util.*;

/** A program to print the words with even number of characters in an array of

10 Strings(Words)*/

public class Even

{//start of class

public void main()

{//start of method

Scanner sc=new Scanner(System.in);

String arr[]=new String[10];

System.out.println("Enter " + arr.length + " words ");

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

System.out.println("Enter the word "+(i+1));

arr[i]=sc.nextLine();

System.out.println(" The original array is ");

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

System.out.println(arr[i]);

/** Words with even number of characters */

System.out.println(" The array words with even number of characters ");

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.

Return datatype:- int

2. compareTo():- This method compares two strings lexicographically.

Return datatype:- int

3. equalsIgnoreCase():- This method compares two strings ignoring the case

differences.

Return datatype:- boolean

4. length():- This method gives the length of the string.

Return datatype:- int


12

Question 7:

Differentiate between the following: [8]

1. equals() and = =

2. charAt() and substring()

3. Binary Search and Linear Search techniques

4. Bubble Sort and Selection Sort

1) equals() ==

1) It is a method.

2) Checks the equality of

constants of the object.

1) It is an operator.

2) Checks the equality of constants of

the object and the references.

2) charAt() substring()

1) It returns a character from

the string at the specified

index.

2) Its return datatype is char.

1) It extracts a part of a string from the

specified index and returns the

extracted part as the new string.

2) Its return datatype is String.

3) Binary Search Linear Search


1) It is efficient when

checking for the specific

element in a large array.

2) The array must be sorted

in ascending or descending

order. It is mandatory.

1) It is not efficient when checking for

the specific element in a large array.

2) The array may or may not be sorted.

It is not mandatory.

4) Bubble sort Selection sort

1) Bubble Sort compares

adjacent elements and swaps

them if they are in wrong

order.

2) Bubble Sort is slower

1) Selection Sort selects the smallest

element from unsorted sub-array and

swaps it with the leftmost unsorted

element.

2) Selection Sort is faster.


13

Question 8:

State the number of bytes occupied by following arrays:

1. char ch[ ]= {‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’, ‘g’};

Ans) 14 bytes

2. double y[ ] = {11.11, 28.6, 38.4};

Ans) 24 bytes
14

Question 9:

State the output of the below string functions:

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:

State Correct or Incorrect for below array declaration statements: [5]

1. int a[10];

Ans) Incorrect

2. int a[] = new int[10];

Ans) Correct

3. int arr[i] = 10;

Ans) Incorrect

4. int a[10] = new int[];

Ans) Incorrect

5. int a = new int[];

Ans) Incorrect

You might also like