Java Programming Questions
1. Give output of following statements:
(i) [Link](“Lucknow”.toUpperCase( ));
(ii) [Link](“England”.charAt(4));
(iii) [Link](“Nice”.equals( “nice”));
(iv) String st = “this is final examination”; [Link]([Link]( ));
2. Give output for following java statements if a = 2, b = 3 initially:
(i) [Link]( a + ( + + b ));
(ii) [Link]( “S”.toLowerCase( ));
(iii) [Link](“India”.compareTo(“country”));
(iv) [Link]([Link]( a, b ));
3. What will be output of the following statements?
String a = “Computer”;
String b = “Applications”;
(i) [Link]( a + b );
(ii) [Link]( [Link]( 3 ));
(iii) [Link]( [Link]( b ));
(iv) [Link]( [Link]( 2 ));
4. Write valid java statements to perform the following tasks on strings:
(i) Extract first 10 characters from a string object str.
(ii) To print the position of the first occurrence of the letter ‘B’ in the string object str.
(iii) Print the length of the string stored in object str.
(iv) Convert the string stored in str in uppercase form.
5. Give output of the following:
(i) [Link](“ComPUter”.toUpperCase( ));
(ii) [Link](“ProGRam”.charAt(4));
(iii) String ss = “My Book”; [Link]([Link]( ));
(iv) [Link](“great”.indexOf(‘e’));
(v) [Link](“great”.equalsIgnoreCase(“GREAT”));
(vi) String ss= “Good”.concat(“Bye”); [Link](ss);
6. Write a single line statement to implement the following if:
String text[ ] = {“Can”, “Don”, “Block”, “Man”, “Bye”, “Cos”, “Sin”, “Good”, “Yes”};
(i) Count the number of string in the data structure text.
(ii) Write a condition to check whether a character identifier ch contains a capital vowel.
(iii) Write a condition to check whether a character identifier ch contains a capital alphabet.
(iv) Write a statement to check string stored in string identifiers ss and na are same.
7. Write valid Java statements to perform following tasks on strings:
(i) Initialize a string object city to store “KOLKATA”.
(ii) Print the number of characters present in the string object city.
(iii) Concatenate “MUMBAI” to the object city and store the output in the object result.
(iv) Extract and print character present at 4th and 7th indexes from the object result.
(v) Compare the object city with “SINGAPORE” and store the boolean result in the variable
res.
(vi) Compare the object city with “SINGAPORE” and store the output in the integer variable
R.
8. On the basis of following array of strings answer the given questions:
String st[ ] = {“SINGAPORE”, “MUMBAI”, “DELHI”, “BANGLORE”};
(i) What is the range of indices of the array st ?
(ii) What is size of the array st ?
(iii) What are the results of st[3].length( ) and st[0].length( ) ?
(iv) What is the result of st[1].toLowerCase( ); ?
(v) What is the result of st[2].reverse( ); ?
(vi) What is the result of “MUMBAI”.replace(‘M’, ‘H’); ?
9. Give output of following program code:
String str = “COMPUTER”;
char ch = [Link]( 9 / 2 );
int a= ch; [Link]( char( a ));
10. Give output of following program code if method is invoked as test( “XYZ”);?
void test( String n )
{
String st = n + “ABC”;
[Link](“n =” + n );
[Link](“st=” + st );
}
11. Write a valid Java statement to assign 8 names of countries in the string array.
12. What do the following functions return for?
String x = “hello”;
String y = “world”;
(i) [Link]( x + y );
(ii) [Link]( [Link]( ));
(iii) [Link]( [Link](3));
(iv) [Link]( [Link]( y ));
(v) [Link]( [Link]( y ));
13. Consider the following program code. Underline the errors and rewrite the correct
program code:
Public Class test
{
public static Void main( String args[ ] )
{
String ns = “10th std ;
[Link](“I am class X student”);
[Link](“I am studying in”ns + “”);
[Link](“This is the output”);
}
}
14. Write a statement to declare and initialize a character array of five capital vowels.
15. State the output of following function when invoked?
public void show( )
{
String val = “examinations”;
for( int i=0; i<12; i++ )
{
for( int j =1 ; j<2 ; j++ )
{
[Link]([Link]( i ) + “ ”);
}
}
}
16. Give the output of following function when invoked?
public void show( )
{
String str = “CALIFORNIA”;
for( int i=0; i<[Link]( ); i++ )
{
for( int j =0; j<=i ; j++ )
{
[Link]( [Link]( j ));
}
[Link](“\n” );
}
}
17. What will be the output of following function when invoked:
public void show( )
{
String str = “COLLEGE”;
for( int i=0; i<[Link]( ); i++ )
{
for( int j =0; j<=i; j++ )
{
[Link]( [Link]( i ));
}
[Link](“\n” );
}
}
18. Give the output for following program code:
String sat = “ExAmInATioN”;
for(int y=0; y<[Link]( ); y+=2)
{
[Link]( [Link]( y ));
}