(a) Write a Java expression for the following: ax 5 + bx 3 + c

ICSE Class 10 Computer Applications PYQ · 2017 Paper · Section A

31 Mark · 2017 · Section A · 2017 Paper

(a) Write a Java expression for the following:
ax5 + bx3 + c

Show SolutionHide Solution↓

a * Math.pow(x, 5) + b * Math.pow(x, 3) + c

(b) What is the value of x1 if x=5?
x1= ++x – x++ + --x

    x1 = ++x – x++ + --x
⇒ x1 = 6 - 6 + 6
⇒ x1 = 0 + 6
⇒ x1 = 6

(c) Why is an object called an instance of a class?

A class can create objects of itself with different characteristics and common behaviour. So, we can say that an Object represents a specific state of the class. For these reasons, an Object is called an Instance of a Class.

(d) Convert following do-while loop into for loop.

int i = 1;
int d = 5;
do {
    d=d*2;
    System.out.println(d);
    i++ ; } while ( i<=5);
int i = 1;
int d = 5;
for (i = 1; i <= 5; i++) {
    d=d*2;
    System.out.println(d);
}

(e) Differentiate between constructor and function.

ConstructorFunction
Constructor is a block of code that initializes a newly created object.Function is a group of statements that can be called at any point in the program using its name to perform a specific task.
Constructor has the same name as class name.Function should have a different name than class name.
Constructor has no return type not even void.Function requires a valid return type.

(f) Write the output for the following:

String s="Today is Test";
System.out.println(s.indexOf('T'));
System.out.println(s.substring(0,7) + " " +"Holiday");

Output of the above code is:

0
Today i Holiday

As the first T is present at index 0 in string s so s.indexOf('T') returns 0. s.substring(0,7) extracts the characters from index 0 to index 6 of string s. So its result is Today i. After that println statement prints Today i followed by a space and Holiday.

(g) What are the values stored in variables r1 and r2:

  1. double r1 = Math.abs(Math.min(-2.83, -5.83));
  2. double r2 = Math.sqrt(Math.floor(16.3));
  1. r1 has 5.83
  2. r2 has 4.0
  1. Math.min(-2.83, -5.83) returns -5.83 as -5.83 is less than -2.83. (Note that these are negative numbers). Math.abs(-5.83) returns 5.83.
  2. Math.floor(16.3) returns 16.0. Math.sqrt(16.0) gives square root of 16.0 which is 4.0.

(h) Give the output of the following code:

String A ="26", B="100";
String D=A+B+"200";
int x= Integer.parseInt(A);
int y = Integer.parseInt(B);
int d = x+y;
System.out.println("Result 1 = " + D);
System.out.println("Result 2 = " + d);

Output of the above code is:

Result 1 = 26100200
Result 2 = 126
  1. As A and B are strings so String D=A+B+"200"; joins A, B and "200" storing the string "26100200" in D.
  2. Integer.parseInt() converts strings A and B into their respective numeric values. Thus, x becomes 26 and y becomes 100.
  3. As Java is case-sensitive so D and d are treated as two different variables.
  4. Sum of x and y is assigned to d. Thus, d gets a value of 126.

(i) Analyze the given program segment and answer the following questions:

for(int i=3;i<=4;i++ ) {
for(int j=2;j<i;j++ ) {
System.out.print("" ); }
System.out.println("WIN" ); }
  1. How many times does the inner loop execute?
  2. Write the output of the program segment.
  1. Inner loop executes 3 times.
    (For 1st iteration of outer loop, inner loop executes once. For 2nd iteration of outer loop, inner loop executes two times.)

  2. Output:
    WIN
    WIN

(j) What is the difference between the Scanner class functions next() and nextLine()?

next()nextLine()
It reads the input only till space so it can read only a single word.It reads the input till the end of line so it can read a full sentence including spaces.
It places the cursor in the same line after reading the input.It places the cursor in the next line after reading the input.
← Previous questionNext question →

Browse the free Computer Applications sample bank