Dear ICSE Students,
Welcome to my blog dedicated to helping you navigate the fascinating world of Computer Science! Whether you're just starting to explore the subject or preparing for your exams, this space is designed to be your go-to resource for guidance, tips, and insights.
I understand that Computer Science can sometimes seem daunting with its intricate concepts and ever-evolving technologies. That's why we're here to simplify things for you.
Feel free to explore my articles, tutorials, and study resources. Don't hesitate to reach out with your questions or suggestions – I'm here to support you every step of the way.
Happy learning!
Keep visiting the page on a regular basis for new contents
Question 1: What does the below image represents?
A
|
A
|
A
|
A
|
A
|
A
|
A
|
A
|
A
|
A
|
A
|
A
|
A
|
A
|
A
|
A
|
A
|
A
|
A
|
A
|
A
|
A
|
A
|
A
|
A
|
Answer: A multidimensional array with 5 Rows and 5 Columns
Description: Multidimensional Array is considered as an Array of Arrays. Such arrays are considered useful when we want to store data as a tabular form like a table with Rows and Columns.
Similar Question
What does the following image represents?
O
|
O
|
O
|
O
|
O
|
O
|
O
|
O
|
O
|
O
|
O
|
O
|
O
|
O
|
O
|
Answer: A multidimensional array with 3 Rows and 5 Columns
Draw a multidimensional array having 3 rows and 6 columns.
Answer:
5
|
5
|
5
|
5
|
5
|
5
|
5
|
5
|
5
|
5
|
5
|
5
|
5
|
5
|
5
|
5
|
5
|
5
|
Question 2: Name the feature due to which Java Compiled codes can run on Linux although the code was developed in MAC.
Answer: Platform Independent
Description: Java is essentially Platform Independent. Change of platform does not affect the original Java program/application.
Similar Question
Name the feature due to which no huge coding is required in Java.
Answer: Light Weight Code.
Description: With Java, no huge coding is required.
Question 3: What is the size of '\n'?
Answer: The size of '\n' is 2 bytes.
Description: '\n' is one of the escape sequence and it is equivalent to one character. Size of character in Java is 2 Bytes and therefore, size of '\n' is 2 bytes.
Similar Question
What is the size of '\t'?
Answer: The size of '\t' is 2 bytes.
Description: '\t' is one of the escape sequence and it is equivalent to one character. Size of character in Java is 2 Bytes and therefore, size of '\t' is 2 bytes.
Question 4: Which arithmetic operator gets the highest precedence while evaluating the following statement?
a1+b1*c1%d1-e1
Answer: *
Description: While evaluating the above statement, the * operator will get the highest precedence because although * and % is given in this expression and they have the same precedence but in terms of their associativity, left to right is followed which means out of * and %, whoever comes first will get the higher precedence.
Similar Question
Which arithmetic operator gets the highest precedence while evaluating the following statement?
7%7*9/3+2
Answer: %
Description: While evaluating the above statement, the % operator will get the highest precedence because although %,* and / is given in this expression and they have the same precedence but in terms of their associativity, left to right is followed which means out of %,* and /, whoever comes first will get the higher precedence.
Question 5: Name some valid Java Keywords.
Answer: Some valid Java Keywords are as follows:
- for
- switch
- if
- boolean
- while
- return
Question 6: What will be the output after executing the below statement?
System.out.println(Math.ceil(6.1)+Math.Floor(-1-2));
Answer: 4.0
Description: The ceil() function returns the smallest whole number greater than on equal to the parameter value. (Rounded Up). Here, as 6.1 is mentioned within ceil, so it is giving 7.
The floor() function returns the largest whole number less than or equal to parameter value. (Rounded Down). Here, as -1-2 was mentioned within floor, so, it is giving -3
So, after calculating 7-3, it is giving 4.
As the Math Functions available in Math Class always returns value of type double due to which, answer shall be in decimal value and hence, answer shall be 4.0.
Similar Question
What will be the output after executing the below statement?
System.out.println(Math.abs(Math.max(-9.6,-2.8));
Answer: 2.8
Description: As per the statement, first the max() function of Math class will execute which will give the output as -2.8. As per statement, now -2.8 is contained with abs() function of Math class which will return the absolute value of the number. Absolute value means positive value.
What will be the output after executing the below statement?
System.out.println(Math.max(16,(Math.max(19,6));
Answer: 19
Description: As per the statement, first the inner max() function of Math class will execute which will give the output as 19 as maximum number among 19 and 6 is 19. Now, the statement will become something like this
System.out.println(Math.max(16,19);
Now, it is clear that the maximum number between 16 and 19 is 19. Hence, 19 is the output.
Question 7: Name the accessor method of the String Class which returns a new string resulting from replacing all occurrences of oldChar with newChar.
Answer: String replace(char oldChar, char newChar)
Description:
replace() method has two parameters namely:
- oldChar - Old Character
- newChar - New Character
Refer to the below program for a clear understanding of the replace() method.
class HelloWorld
{
public static void main(String[] args)
{
String S="Computer Science";
String S1=S.replace('e','a');
//replaces all occurrences of 'e' to 'a'
System.out.println(S1);
}
}
After executing the above code, output shall be:
Computar Scianca
Similar Question
Give a demonstration of the accessor method of the String Class which returns a new string resulting from replacing all occurrences of oldChar with newChar.
[Hint: Information Technology shoule be displayed as Infermatien Technelegy]
class HelloWorld
{
public static void main(String[] args)
{
String S="Information Technology";
String S1=S.replace('o','e');
//replaces all occurrences of 'o' to 'e'
System.out.println(S1);
}
}
After executing the above code, output shall be:
Infermatien Technelegy
Question 8: Will it be true to say that switch statement supports floating point constants?
Answer: No
Description: Switch statements does not support floating-point constants or expressions as case labels. This limitation is due to the nature of how switch statements are typically implemented and optimized for efficiency. Many compilers prohibits the use of floating-point constants in switch statements. If used, it results in a compilation error or a warning.
Similar Question
Switch statement checks for equality between the input and case labels (True/False)
True
In Switch case, break is used to exit from the Switch block (True/False)
True
In Switch case, case labels are unique (True/False)
True
Question 9: What will be the output of the following statement?
public class Operator
{
public static void main(String[]args)
{
char ch[]={'A','E','I','O','U'};
System.out.println(ch[1]*2);
}
}
Answer: 138
Description: In this code, a character array having 5 elements was declared. In the print statement, it is written as ch[1]*2 which means that the element at the 1st Index needs to be multiplied by 2. The element at 1st index is E because in array, index position begin with 0. Now, the program will first get the ASCII code of E which is 69.
Now, as per the statement, it is mentioned as ch[1]*2 which means, 69 * 2 resulting to which, 138 will come as output.
Similar Question
What will be the output of the following statement?
public class Operator
{
public static void main(String[]args)
{
int a[]={7,12,5,6,7,9,15,92};
System.out.println(a[5]*12);
}
}
Answer: 108
Description:
Index Position
|
a[0]
|
a[1]
|
a[2]
|
a[3]
|
a[4]
|
a[5]
|
a[6]
|
a[7]
|
a
|
7
|
12
|
5
|
6
|
7
|
9
|
15
|
92
|
Element at index position a[5] is 9. When it will get multiplied by 12, it will give output as follows:
108
Write a java program to print the length of the following array:
char x[]={'A','E','I','O','U'};
Answer:
public class Operator
{
public static void main(String[]args)
{
char x[]={'A','E','I','O','U'};
System.out.println("Length of the array is "+x.length);
}
}
After execution, the output will be as follows:
Length of the array is 5
Question 10. How many times, the following loop statements will execute?
a) for(int a = 11;a<=30;a+=2)
Answer:
The above loop when executed will produce the following output:
Hence, we can see that the loop will execute for 10 times.
b) for(int a = 11;a<=30;a+=3)
Answer:
The above loop when executed will produce the following output:
Hence, we can see that the loop will execute for 7 times.
c) for(int a = 11;a<20;a++)
Answer:
The above loop when executed will produce the following output:
Hence, we can see that the loop will execute for 9 times.
d) for(int a = 11;a<=21;a++)
Answer:
The above loop when executed will produce the following output:
Hence, we can see that the loop will execute for 11 times.
Question 11. Suppose, a single dimensional array has 5 elements in it. Write an array declaration to initialize the last element of the array to 50.
Answer: int a[4] = 50;
Description: Below is the structure of an array. In the following array, there are 5 elements in it. Array index positions starts from 0 and that's why, it is clearly seen that the first index position is 0 and last index position is 4. In the following array structure,
1st element is containing 20
2nd element is containing 21
3rd element is containing 30
4th element is containing 8
5th element is containing 50
Index
|
a[0]
|
a[1]
|
a[2]
|
a[3]
|
a[4]
|
a
|
20
|
21
|
30
|
8
|
50
|
Elements
|
1
|
2
|
3
|
4
|
5
|
Similar Question
Suppose a single dimensional array has 6 elements in it. Write an array declaration to initialize the 3rd element of the array to 70.
Answer: int b[2] = 70;
Suppose a single dimensional array has 9 elements in it. Write an array declaration to initialize the 2nd last element of the array to "Rohit".
Answer: String name[7] = "Rohit";
Description: Rohit is a word, hence String data type has been taken. name is taken as the name of the array. Total element in array is 9. As array elements starts from 0 and hence, the last index position of the array is 8.
1st element
|
|
|
|
|
|
|
|
Last element
|
0
|
1
|
2
|
3
|
4
|
5
|
6
|
7
|
8
|
|
|
|
|
|
|
|
Rohit
|
|
Question 12. Write a method prototype for the method display which accepts two integer arguments and returns true/false:
Answer: boolean display(int a, int b)
Description: As asked in question, a method prototype for the method namely display having two integer arguments were passed as parameters(). As integer variable names were not there, so we created our own variable namely a and b. At last, it is mentioned that the method should return the value in true/false and due to which boolean data type has been taken as we all know that boolean data type can store true or false values.
Similar Question
Write the prototype of the function verifies that accepts character chr and integer x and returns true or false.
Answer: boolean verifies(char chr, int x)
Write the function prototype for the function Poschar which takes a string argument and a character argument and returns an integer value.
Answer: int Poschar(String str, char ch)
Question 13. Name the statement that brings the control back to the calling method.
Answer: return
Description:
The “return” keyword can help in transferring control from one method to the method that called it. Since the control jumps from one part of the program to another, the return is also a jump statement like break and continue.
“return” is a reserved keyword means we can’t use it as an identifier.
Similar Question
Name the statement that terminates the current loop or switch statement.
Answer: break
Description: The break statement is one of the jump statements that terminates the current loop or switch statement. The execution then continues from the statement immediately following the current loop or switch statement. The break statement consists of the keyword break followed by a semicolon.
Syntax of break statement:
break;
Name the statement that instructs the computer to skip the rest of the current iteration of the loop.
Answer: continue
Description: The continue statement is one of the jump statements that instructs the computer to skip the rest of the current iteration of the loop. Unlike the break statement which forces termination of the loop, the continue statement forces the next iteration of the loop to take place, skipping any instructions in between. The continue statement consists of the keyword continue followed by a semicolon.
Syntax of continue statement:
continue;
Question 14. Name the data type that can store true or false values.
Answer: boolean
Description:
boolean data type is used to store logical values. It can store only true or false.
Although the boolean data type needs only one bit of storage, the Java compiler reserves 8 bits (1 byte) for it.
Example of boolean Data type:
public class booleandemo
{
public static void main(String[]args)
{
boolean x = true;
boolean y = false;
System.out.println(x);
System.out.println(y);
}
}
After executing the above code, following will be the output:
Question
15. Write a program to accept a string, then convert it into uppercase and
finally, display the first alphabet of each word.
Answer:
import java.util.*;
class Hello
{
public
static void main(String[]args)
{
Scanner
sr = new Scanner(System.in);
System.out.println("Enter a string:");
String S
= sr.nextLine();
String
Su = S.toUpperCase();
System.out.println(Su.charAt(0));
for(int
i = 0;i<Su.length();i++)
{
if(Su.charAt(i)== ' ')
{
System.out.println(Su.charAt(i+1));
}
}
}
}
Let us Dry run the program:
1st, Enter a string will be displayed.
sr.nextLine(); will accept the input from user and
will store the string in the variable namely S of String data type.
S.toUpperCase(); will convert the string into
uppercase letters and the converted uppercase letters will be stored in the
variable namely Su of String data type.
System.out.println(Su.charAt(0)); will print the first
character of the string.
A loop have been created to ensure that it can read
the characters from index number 0 till last index number. (index of an array
is always -1 than the length of the array). It is accomplished using the code
below:
for(int i = 0;i<Su.length();i++)
The statement i.e., if(Su.charAt(i)== ' ') within the for loop
block will search for a space.
If it finds space, then the following statement will
get executed or else the loop will keep on executing till it reaches the last index position:
System.out.println(Su.charAt(i+1));
The above statement will print the character whose index is next to the index in which space is present.
In this way, the program will print the first
alphabet of each word on the screen.
Question 16. Consider the following program
public class hello
{
void swap(int i, int j)
{
i = j;
j = k;
int k = 0;
k = i;
System.out.println(i);
}
}
What will be the output of the above program? If any problem is encountered, rewrite the program for getting desired output.
Answer:
The above program when executed will show error as follows:
Undeclared variable: k
The corrected code should be like this:
public class hello
{
void swap(int i, int j)
{
int k = 0;
k = i;
i = j;
j = k;
System.out.println(i);
}
}
Description: Here, problem is with the arrangement of statement within the swap method. Simply, we need to change the ordering of the statements written inside the swap method.
Question 17. Write a Java program to print the length of the array.
Answer:
public class arraylength
{
public static void main(String[]args)
{
int arr[]={2,6,8,12,37};
System.out.println("Length of the array is: "+arr.length);
}
}
After executing the above program, following will be the output:
Length of the array is: 5
Question 18. What will be the output after executing the below program?
public class hello
{
public static void main(String[]args)
{
int x = 2,y = 3;
y*=x++ - ++y + ++x;
System.out.println("x= "+x);
System.out.println("y= "+y);
}
}
Answer:
Description:
y = y*2-4+4;
y = 3*2-4+4;
y = 6-4+4;
y = 2+4
y = 6
Present value of x is 4 and after evaluation, value of y became 6.
(Operator precedence is taken care of while evaluating the above expression)
Question 19. XYZ is a student of class X and while executing the code, error have been encountered. Identify the error and rewrite the code.
public class hello
{
public static void main(String[]args)
{
boolean inp = true;
switch(inp)
{
case 1:
System.out.println("Congratulations");
case 2:
System.out.println("Better luck next time");
}
}
}
Answer:
The above program when executed will produce the following error:
Incompatible types: int cannot be converted to boolean.
The corrected code shall be:
public class hello
{
public static void main(String[]args)
{
int inp = 1;
switch(inp)
{
case 1:
System.out.println("Congratulations");
case 2:
System.out.println("Better luck next time");
}
}
}
Question 20. Write a Java program to print the value of x = a2+2ab where a varies from 1.0 to 6.0 with increment of 2.0 and b=3.0 is a constant.
Answer:
public class hello
{
public static void main(String[]args)
{
final double b = 3.0;
for(double a = 1.0;a<=6.0;a+=2)
{
System.out.println("Value of x when a is "+a+" : "+Math.pow(a,2)+2*a*b);
}
}
}
After executing the above program, the output shall be:
Value of x when a is 1.0 : 1.06.0
Value of x when a is 3.0 : 9.018.0
Value of x when a is 5.0 : 25.030.0
Question 21. Convert the following complex program into a
simple program:
class Hello
{
public static void main(String[]args)
{
char character = 'a';
if(character == 'a')
{
System.out.println("Vowel");
}
if(character == 'A')
{
System.out.println("Vowel");
}
}
}
Answer:
The simplest form of the same program is shown below:
class Hello
{
public static void main(String[]args)
{
char character = 'a';
if(character == 'a' || character == 'A')
{
System.out.println("Vowel");
}
}
}
Question 22. How many times, the following loop will execute?
Write the output of the code.
class Hello
{
public static void main(String[]args)
{
int x = 10;
while(true)
{
System.out.println(x++*2);
if(x%3==0)
{
break;
}
}
}
}
Answer: The above loop will execute for 2 times.
Output shall be as follows:
20
22
Question 23. What will be the output after executing the
below program?
class Hello
{
public static void main(String[]args)
{
String a = "Guava", b = "Green";
System.out.println(a.charAt(0)==b.charAt(0));
System.out.println(a.compareTo(b));
}
}
Answer: After executing the above program, the output
shall be:
true
3
Description:
String a
|
String b
|
|
Guava
|
Green
|
|
The charAt()
method returns the character at the specified index in a string.
The index of
the first character is 0, the second character is 1, and so on.
|
Character at
0 index is G
|
Character at
0 index is G
|
G==G, hence it will give
result as true
|
The
compareTo() method compares two strings lexicographically.
The
comparison is based on the Unicode value of each character in the strings.
|
Character at
0 index is G
ASCII code of
G is 71
|
Character at
0 index is G
ASCII code of
G is 71
|
71-71=0
compareTo()
method will now go to index 1 and so on until it doesn’t finds any positive
integer value to be displayed.
|
Character at 1
index is u
ASCII code of
u is 117
|
Character at 1
index is r
ASCII code of
u is 114
|
117-114=3
|
Question 24. What will be the output after executing the below program?
class Hello
{
public static void main(String[]args)
{
char ch = 'B';
char chr = Character.toLowerCase(ch);
int n = (int) chr-10;
System.out.println((char)n+"\t"+chr);
}
}
Answer: After executing the above program, the output
shall be:
X b
Description:
ch namely variable is holding B in it.
With the help of string accessor method i.e. Character.toLowerCase(ch), B have been converted to b and this lowercase letter b have been stored into chr.
The next line i.e. int n = (int) chr-10 states that the value contained within chr needs to be converted into integer and then, it should be subtracted by 10.
chr is a variable containing b in it. ASCII code of b is 98 and when it is subtracted by 10, it becomes 88. So, now 88 is stored into the new integer variable namely n.
In the print statement, it can be seen that the integer variable n is again getting converted to character. n is holding 88 and character X have the ASCII code 88. So, first X will get printed. Then "\t", the escape sequence character will insert a tab and finally, value stored in chr will get displayed which is b. Hence, output is coming as:
X b
Question 25. Write a java program to accept a number using
string data type. Then, convert the string to a numerical value so that square
root of the converted value can be obtained.
Answer:
import java.util.Scanner;
class Hello
{
public static void main(String[]args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number");
String s = sc.nextLine();
double x = Double.parseDouble(s);
double r = Math.sqrt(x);
System.out.println(r);
}
}
Description:
As it is asked, user input have been stored in a String variable namely s. Say for example, 25 have been given by user that is stored in s. Double.parseDouble() is used to parse String to double means that value stored as string can be converted using this method. Value of 25 which was stored as a string in variable namely s have been converted into double value and it is now stored in a variable called x. Now, x is containing 25. Then with the help of Math.sqrt() function, square root of x have been determined and it is stored in a new variable r. Finally, r is displayed.
Question 26. Write a Java Program to show a multiplication table. Ensure that the number of which multiplication table needs to be shown is accepted from the user.
Answer:
import java.util.Scanner;
class hello
{
public static void main(String[]args)
{
Scanner KBD = new Scanner(System.in);
System.out.println("Enter a number");
int number = KBD.nextInt();
for(int i = 1;i<=10;i++)
{
System.out.println(number+" X "+i+" = "+number*i);
}
}
}
Question 27. Write a Java Program to accept a string from user, convert it into uppercase and then replace the vowels with the next character following it.
Hint:
Input: Computer
Output: CPMPVTFR
Answer:
import java.util.Scanner;
class Vowelreplace
{
public static void main(String[]args)
{
Scanner sr = new Scanner(System.in);
int i,len;
String word,newword = "";
char ch, ch1;
System.out.println("Enter a Word:");
word = sr.nextLine();
word = word.toUpperCase();
len = word.length();
for(i = 0;i<len;i++)
{
ch = word.charAt(i);
if(ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U')
{
char nextchar = (char)(ch+1);
newword = newword+nextchar;
}
else
{
newword = newword+ch;
}
}
System.out.println("The converted string is:"+newword);
}
}
Question 28. Consider the following program segment and answer the questions given below:
int x[][] = {{2,4,5,6},{5,7,8,1},{34,1,10,9}};
(a) What is the position of 34?
Answer:
In order to find the position of an element, first, let us draw the structure of an array:
|
|
Columns
|
|
|
0
|
1
|
2
|
3
|
Rows
|
0
|
2
|
4
|
5
|
6
|
1
|
5
|
7
|
8
|
1
|
2
|
34
|
1
|
10
|
9
|
As per the above diagram, we can see the location of 34. 34
is located at an intersection of 2 and 0. In multidimensional array, index
position of row is written first and then, index position of column is written.
Here, row is 2 and column is 0. Hence, location
of 34 is x[2][0]
(b) What is the result of x[2][3] + x[1][2]?
Answer:
x[2][3] = 9
x[1][2] = 8
9+8 = 17
(c) What is the result of x[0][2] * x[2][3]?
Answer:
x[0][2] = 5
x[2][3] = 9
5*9 = 45
(d) What is the result of x[0][2] * x[2][3] + x[2][2]?
Answer:
x[0][2] = 5
x[2][3] = 9
x[2][2] = 10
5*9+10 = 55
Question 29. Write a java program to find the number of rows and columns in the following array structure:
Answer:
public class Operator
{
public static void main(String[]args)
{
char x[][]={{'S','U','N','D','A','Y'},{'M','O','N','D','A','Y'}};
System.out.println("Number of row is "+x.length);
System.out.println("Number of column is "+x[0].length);
}
}
Question 30. Write a java program to print the following
pattern
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
Answer:
class HelloWorld
{
public static void main(String[] args)
{
for(int i = 1;i<=5;i++)
{
for(int j = 1;j<=i;j++)
{
System.out.print(i+" ");
}
System.out.println();
}
}
}
Question 31. Write a java program to print the following pattern
1 1 1 1 1
2 2 2 2 2
3 3 3 3 3
4 4 4 4 4
5 5 5 5 5
Answer:
class HelloWorld
{
public static void main(String[] args)
{
for(int i = 1;i<=5;i++)
{
for(int j = 1;j<=5;j++)
{
System.out.print(i+" ");
}
System.out.println();
}
}
}
Question 32. Write a java program to print the following pattern
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
class HelloWorld
{
public static void main(String[] args)
{
for(int i = 1;i<=5;i++)
{
for(int j = 1;j<=5;j++)
{
System.out.print(j+" ");
}
System.out.println();
}
}
}
Question 33. Write a java program to print the following pattern
B
LL
UUU
EEEE
class HelloWorld
{
public static void main(String[] args)
{
String Word = "BLUE";
for(int i = 0;i<Word.length();i++)
{
for(int j = 0;j<=i;j++)
{
System.out.print(Word.charAt(i));
}
System.out.println();
}
}
}
Question 34. Define a class series to display the sum of the series given below:
x1+x2+x3..........xn terms
Let x = 1 and n = 10
class Series
{
public static void main(String[] args)
{
int x = 1;
double sum = 0;
for(int n = 1;n<=10;n++)
{
sum = sum+Math.pow(x,n);
}
System.out.println("Sum of the series is "+sum);
}
}