0% found this document useful (0 votes)
3 views17 pages

Java Programming Concepts and Exercises

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views17 pages

Java Programming Concepts and Exercises

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Sec on A

Ques on 1(i)

Return data type of isLe er(char) is ............... .

1. Boolean

2. boolean

3. bool

4. char

Answer

boolean

Reason — isLe er() method returns true if the specified character is a le er else, it returns
false. Thus, the return data type of isLe er(char) is boolean.

Ques on 1(ii)

Method that converts a character to uppercase is ............... .

1. toUpper()

2. ToUpperCase()

3. toUppercase()

4. toUpperCase(char)

Answer

toUpperCase(char)

Reason — Method that converts a character to uppercase is toUpperCase(char) method.

Ques on 1(iii)

Give output of the following String methods:

"SUCESS".indexOf('S') + "SUCCESS".lastIndexOf('S')

1. 0

2. 5

3. 6

4. -5

Answer
6

Reason — indexOf() returns the index of the first occurrence of the specified character
within the string and lastIndexOf() returns the index of the last occurrence of the specified
character within the string. Since a string begins with index 0, the given methods are
evaluated as follows:

"SUCESS".indexOf('S') + "SUCCESS".lastIndexOf('S')
⇒0+6
⇒6

Ques on 1(iv)

Corresponding wrapper class of float data type is ............... .

1. FLOAT

2. float

3. Float

4. Floa ng

Answer

Float

Reason — Corresponding wrapper class of float data type is Float.

Ques on 1(v)

............... class is used to convert a primi ve data type to its corresponding object.

1. String

2. Wrapper

3. System

4. Math

Answer

Wrapper

Reason — Wrapper class is used to convert a primi ve data type to its corresponding object.

Ques on 1(vi)

Give the output of the following code:

[Link]("Good".concat("Day"));
1. GoodDay

2. Good Day

3. Goodday

4. goodDay

Answer

GoodDay

Reason — concat() method is used to join two strings. Thus, "Good" and "Day" are joined
together and printed as "GoodDay".

Ques on 1(vii)

A single dimensional array contains N elements. What will be the last subscript?

1. N

2. N - 1

3. N - 2

4. N + 1

Answer

N-1

Reason — The index of an array begins from 0 and con nues ll Size - 1. Thus, if an array has
N elements, the last subscript/index will be N - 1.

Ques on 1(viii)

The access modifier that gives least accessibility is:

1. private

2. public

3. protected

4. package

Answer

private

Reason — A data member or member method declared as private is only accessible inside
the class in which it is declared. Thus, it gives the least accessibility.

Ques on 1(ix)
Give the output of the following code :

String A = "56.0", B = "94.0";

double C = [Link](A);

double D = [Link](B);

[Link]((C+D));

1. 100

2. 150.0

3. 100.0

4. 150

Answer

150.0

Reason — parseDouble() method returns a double value represented by the specified string.
Thus, double values 56.0 and 94.0 are stored in C and D, respec vely. Both C and D are
added and 150.0 (56.0 + 94.0) is printed on the screen.

Ques on 1(x)

What will be the output of the following code?

[Link]("Lucknow".substring(0,4));

1. Lucknow

2. Luckn

3. Luck

4. luck

Answer

Luck

Reason — The substring() method returns a substring beginning from the star ndex and
extending to the character at index endIndex - 1. Since a string index begins at 0, the
character at star ndex (0) is 'L' and the character at the endIndex (4-1 = 3) is 'k'. Thus, "Luck"
is extracted and printed on the screen.

Sec on B

Ques on 2
Define a class to perform binary search on a list of integers given below, to search for an
element input by the user, if it is found display the element along with its posi on,
otherwise display the message "Search element not found".

2, 5, 7, 10, 15, 20, 29, 30, 46, 50

import java.u [Link];

public class KboatBinarySearch

public sta c void main(String args[]) {

Scanner in = new Scanner([Link]);

int arr[] = {2, 5, 7, 10, 15, 20, 29, 30, 46, 50};

[Link]("Enter number to search: ");

int n = [Link]();

int l = 0, h = [Link] - 1, index = -1;

while (l <= h) {

int m = (l + h) / 2;

if (arr[m] < n)

l = m + 1;

else if (arr[m] > n)

h = m - 1;

else {

index = m;

break;

}
if (index == -1) {

[Link]("Search element not found");

else {

[Link](n + " found at posi on " + index);

Output
Ques on 3

Define a class to declare a character array of size ten. Accept the characters into the array
and display the characters with highest and lowest ASCII (American Standard Code for
Informa on Interchange) value.
EXAMPLE :
INPUT:
'R', 'z', 'q', 'A', 'N', 'p', 'm', 'U', 'Q', 'F'
OUTPUT :
Character with highest ASCII value = z
Character with lowest ASCII value = A

import java.u [Link];

public class KboatASCIIVal

public sta c void main(String[] args)

Scanner in = new Scanner([Link]);

char ch[] = new char[10];

int len = [Link];


[Link]("Enter 10 characters:");

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

ch[i] = [Link]().charAt(0);

char h = ch[0];

char l = ch[0];

for (int i = 1; i < len; i++)

if (ch[i] > h)

h = ch[i];

if (ch[i] < l)

l = ch[i];

[Link]("Character with highest ASCII value: " + h);

[Link]("Character with lowest ASCII value: " + l);

}
Output

Ques on 4

Define a class to declare an array of size twenty of double datatype, accept the elements
into the array and perform the following :

Calculate and print the product of all the elements.


Print the square of each element of the array.

import java.u [Link];

public class KboatSDADouble

public sta c void main(String[] args)

Scanner in = new Scanner([Link]);

double arr[] = new double[20];

int l = [Link];

double p = 1.0;
[Link]("Enter 20 numbers:");

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

arr[i] = [Link]();

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

p *= arr[i];

[Link]("Product = " + p);

[Link]("Square of array elements :");

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

double sq = [Link](arr[i], 2);

[Link](sq + " ");

Output
Ques on 5

Define a class to accept a string, and print the characters with the uppercase and lowercase
reversed, but all the other characters should remain the same as before.

EXAMPLE:
INPUT : WelCoMe_2022
OUTPUT : wELcOmE_2022
import java.u [Link];

public class KboatChangeCase

public sta c void main(String args[])

Scanner in = new Scanner([Link]);

[Link]("Enter a string:");

String str = [Link]();

int len = [Link]();

String rev = "";

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

char ch = [Link](i);

if ([Link] er(ch))

if([Link](ch))

rev += [Link](ch);

else

rev += [Link](ch);

else

{
rev += ch;

[Link](rev);

Output

Ques on 6

Define a class to declare an array to accept and store ten words. Display only those words
which begin with the le er 'A' or 'a' and also end with the le er 'A' or 'a'.
EXAMPLE :
Input : Hari, Anita, Akash, Amrita, Alina, Devi Rishab, John, Farha, AMITHA
Output: Anita
Amrita
Alina
AMITHA

import java.u [Link];


public class KboatWords

public sta c void main(String args[])

Scanner in = new Scanner([Link]);

String names[] = new String[10];

int l = [Link];

[Link]("Enter 10 names : ");

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

names[i] = [Link]();

[Link]("Names that begin and end with le er A are:");

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

String str = names[i];

int len = [Link]();

char begin = [Link]([Link](0));

char end = [Link]([Link](len - 1));

if (begin == 'A' && end == 'A') {

[Link](str);

}
Output

Ques on 7

Define a class to accept two strings of same length and form a new word in such a way that,
the first character of the first word is followed by the first character of the second word and
so on.
Example :
Input string 1 – BALL
Input string 2 – WORD

OUTPUT : BWAOLRLD

import java.u [Link];

public class KboatStringMerge

public sta c void main(String args[])

{
Scanner in = new Scanner([Link]);

[Link]("Enter String 1: ");

String s1 = [Link]();

[Link]("Enter String 2: ");

String s2 = [Link]();

String str = "";

int len = [Link]();

if([Link]() == len)

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

char ch1 = [Link](i);

char ch2 = [Link](i);

str = str + ch1 + ch2;

[Link](str);

else

[Link]("Strings should be of same length");

Output

You might also like