0% found this document useful (0 votes)
68 views5 pages

Revision Test (Key Answer)

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

Revision Test (Key Answer)

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

Key answer

General Instructions:-

 Read the questions carefully before answering.


 All the answers should be written on the Answer sheets provided separately
 Intended marks are indicated against each question.
 Answers should be written legibly.
 A neat presentation is expected.

Section – A [20 marks]


Attempt all the questions.

Question 1:
Choose the correct answer:
1. Assertion (A): Strings can be added using the operator ‘+’.
Reason (R ) : In java addition of constants of all data types is carried out in the same
way using ‘+ operator to obtain similar results.
a. Assertion (A) and Reason (R ) are true and Reason (R ) is the correct explanation
b. Assertion (A) and Reason (R ) are false
c. Assertion (A) is true and Reason (R ) is false
d. Assertion (A) false and Reason (R ) is true

2. Strings are
a. Mutable
b. Immutable
c. Primitive data types
d. None of the above.

3. Identify the java concept depicted in the picture given below:

a. Strings
b. Double dimensional arrays
c. Encapsulation
d. Message passing

Page 1 of 5
4. What will the code print?
int arr[] = new int [5];
System.out.println(arr);
a. 0
b. value stored in arr[0]
c. 0000
d. garbage value

5. How much memory is required for following array


short m[ ]=new short[4];
a. 2 bytes
b. 8 bytes
c. 16 bytes
d. 12 bytes

6. What will be output when the following code is executed.


char a[ ]={‘a’,’b’,’c’,’d’,’e’};
System.out.println(a[0]+a[2]);
a. 169
b. 196
c. ab
d. Error

7. The output of the following code will be:


String p = “29.4”;
int q= 12;
float c= Float.parseFloat(p)+q;
System.out.println(“The answer is “+c);
a. The answer is 41.4
b. The answer is 29.412
c. The answer is 1229.4
d. Error

8. The index of a string ............... .


a. ranges from 0 to the length -1 of the string
b. ranges from 0 to the length of the string
c. ranges from 1 to the length of the string
d. ranges from 1 to the length -1 of the string

9. Which one of the given statements is true for the following statement?
string1.compareTo(string2)
a. if string1 > string2 the result will be a negative integer i.e. < 0.
b. if string1 > string2 the result will be a positive integer i.e. > 0.
c. if string1 > string2 the result will be 0.
d. None of the above

10. A linear search ...........


a. can be used with sorted arrays only
b. can be used with unsorted arrays only
c. can be used with both sorted and unsorted arrays
d. cannot be used with arrays
Page 2 of 5
Annual Exam-Gr3-ICSE-SST-MAR 20

Question 2:
1. Write any two differences between Bubble sort and Selection sort. [2]

2. Consider the following array [2]


int m[ ] = {4,6,8,12,16,3,9,7}; Write java snippet to add 3 to each element of the array
and display the new array.

int m[ ] = {4,6,8,12,16,3,9,7};

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

m[i]=m[i]+3;
3. What will be the value of s ? [2]
int m = 987654321;
String s = “ “;
while (m != 0) {

int digit = m % 10;


s = s + digit;
m = m / 10;}
123456789

4. Write valid java statement to perform the following on Strings [2]


i) Extract 10 characters starting from the first index
ii) To print the position of the first occurrence of letter ‘R’ in the string city
i) for(int i=1;i<=10;i++)
System.out.println(s.charAt(0));
ii) System.out.println(city.indexOf(‘R’));
5. What will be the output of the following [2]
System.out.println(“TEAR”.compareTo(”FEAR”));
System.out.println(“INDIA”.compareTo(“INK”));

14
-7

Page 3 of 5
Section-B
Attempt all questions from this Section.

Each program should be written in such a way that it clearly depicts the logic of the
program.
Question 3:
Write a program to input a string and print each word of the string in the reverse order.
Sample Input:
Enter a string: My name is Raman
Sample Output
yM eman si namaR

import java.util.Scanner;

public class ReverseWordsManual {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

// Input the string


System.out.print("Enter a string: ");
String input = scanner.nextLine();

String result = "";


int start = 0;
for (int i = 0; i <= input.length(); i++) {

if (i == input.length() || input.charAt(i) == ' ') {


// Reverse the current word
for (int j = i - 1; j >= start; j--) {
result += input.charAt(j);
}

if (i != input.length()) {
result += " ";
}

start = i + 1;
}
}

System.out.println("Reversed words: " + result);


}
}

Page 4 of 5
Annual Exam-Gr3-ICSE-SST-MAR 20

Question 4:
Define a class to accept four characters in two different one dimensional arrays x and y.
Check and print the message “identical” if the characters at the corresponding index positions
of x and y are same.
Example: Input: x[]={'x', 'y', 'z', 'w'} and y[]={'x', 'y', 'z', 'w'} Output: identical

import java.util.Scanner;

public class IdenticalArrays {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

char[] x = new char[4];


char[] y = new char[4];
System.out.println("Enter 4 characters for array x:");
for (int i = 0; i < 4; i++) {
x[i] = scanner.next().charAt(0);
}
System.out.println("Enter 4 characters for array y:");
for (int i = 0; i < 4; i++) {
y[i] = scanner.next().charAt(0);
}
boolean identical = true;
for (int i = 0; i < 4; i++) {
if (x[i] != y[i]) {
identical = false;
break;
}
}

if (identical) {
System.out.println("identical");
} else {
System.out.println("not identical");
}
}
}

*****

Page 5 of 5

You might also like