ICSE Java

Section - A

Question 1

Choose the correct answers to the questions from the given options.
  1. question-imgName the feature of java depicted in the above picture.
    1. Encapsulation
    2. Inheritance
    3. Abstraction
    4. Polymorphism

    Answer: b. Inheritance
  2. The expression which uses `>=` operator is known as:
    1. relational
    2. logical
    3. arithmetic
    4. assignment

    Answer: a. relational
  3. Ternary operator is a:
    1. logical operator
    2. arithmetic operator
    3. relational operator
    4. conditional operator

    Answer: d. conditional operator
  4. When primitive data type is converted to a corresponding object of its class, it is called:
    1. Boxing
    2. Unboxing
    3. explicit type conversion
    4. implicit type conversion

    Answer: a. Boxing
  5. The number of bytes occupied by a character array of 10 elements
    1. 20 bytes
    2. 60 bytes
    3. 40 bytes
    4. 120 bytes

    Answer: a. 20 bytes
  6. The method of Scanner class used to accept a double value is:
    1. nextInt()
    2. nextDouble()
    3. next()
    4. nextInteger()

    Answer: b. nextDouble()
  7. Among the following which is a keyword
    1. every
    2. all
    3. case
    4. each

    Answer: c. case
  8. The output of Math.round(6.6) + Math.ceil(3.4) is:
    1. 9.0
    2. 11.0
    3. 10.0
    4. 11

    Answer: b. 11.0
  9. Name the type of error, if any in the following statement: System.out.print("HELLO")
    1. logical
    2. no error
    3. runtime
    4. syntax

    Answer: b. no error
  10. Java statement to access the 5th element of an array is
    1. X[4]
    2. X[5]
    3. X[3]
    4. X[0]

    Answer: a. X[4]
  11. The output of “Remarkable”.substring(6) is:
    1. mark
    2. emark
    3. marka
    4. able

    Answer: d. able
  12. Which of the following is the wrapper class for the data type char?
    1. String
    2. Char
    3. Character
    4. Float

    Answer: c. Character
  13. Name the package that contains wrapper classes:
    1. java.lang
    2. java.util
    3. java.io
    4. java.awt

    Answer: a. java.lang
  14. Constructor overloading follows which principle of Object Oriented programming?
    1. Inheritance
    2. Polymorphism
    3. Abstraction
    4. Encapsulation

    Answer: b. Polymorphism
  15. Which of the following is a valid Integer constant:
    1. 4
    2. 4.0
    3. 4.3f
    4. "four"
    1. Only 1
    2. 1. and 3.
    3. 2. and 4.
    4. 1. and 2.

    Answer: a. Only 1
  16. The method compareTo() returns __________ when two strings are equal and in lowercase:
    1. true
    2. 0
    3. 1
    4. false

    Answer: b. 0
  17. Assertion(A): In java statements written in lower case letter or upper case letter are treated as the same
    Reason(R): Java is a case sensitive language.
    1. Both Assertion (A) and Reason (R) are true and Reason (R) is a correct explanation of Assertion (A)
    2. Both Assertion (A) and Reason (R) are true and Reason (R) is not a correct explanation of Assertion(A)
    3. Assertion (A) is true and Reason (R) is false
    4. Assertion (A) is false and Reason (R) is true

    Answer: d. Assertion (A) is false and Reason (R) is true
  18. Read the following text, and choose the correct answer:

    A class encapsulate Data Members that contains the information necessary to represent the class and Member methods that perform operations on the data member.

    What does a class encapsulate?
    1. Information and operation
    2. Data members and Member methods
    3. Data members and information
    4. Member methods and operation

    Answer: b. Data members and Member methods
  19. Assertion(A): call by value is known as pure method
    Reason(R): The original value of variable does not change as operation is performed on copied values.
    1. Both Assertion (A) and Reason (R) are true and Reason (R) is a correct explanation of Assertion (A)
    2. Both Assertion (A) and Reason (R) are true and Reason (R) is not a correct explanation of Assertion (A)
    3. Assertion (A) is true and Reason (R) is false
    4. Assertion (A) is false and Reason (R) is true

    Answer: a. Both Assertion (A) and Reason (R) are true and Reason (R) is a correct explanation of Assertion (A)
  20. What Will be the output for:

    System.out.print(Character.toLowerCase('1'));
    1. 0
    2. 1
    3. A
    4. true

    Answer: b. 1

Question 2

  1. Write the Java expression for (p + q)2

    Answer

    (p + q) * (p + q)
    
  2. valuate the expression when the value of x = 2:

    x = x ++ + ++ x + x
    

    Answer

    10

    Explanation

    The following expression/statement can be written as:

    x = (x ++) + (++ x) + x
    

    Now, x++ which is post-increment will return the value of x, 2, and then increment it to 3. So now we have

    x = 2 + (++x) + x // now x is 3
    

    Next, ++x which is pre-increment will return the value of x after incrementing, so ++x = 3 + 1 = 4. Now we have:

    x = 2 + 4 + x
    or
    x = 6 + x
    // x is now 4
    

    Hence, x = 6 + 4 which is 10.

  3. The following code segment should print “You can go out” if you have done your homework (dh) and cleaned your room(cr). However, the code has errors. Fix the code so that it compiles and runs correctly.

    boolean dh = True;
    
    boolean cr= true;
    if (dh && cr)
    System.out.println("You cannot go out");
    else
    System.out.println("You can go out")
    ``       ### Answer
    1. The variable declaration of `dh` is setting the value of boolean to `True` which is incorrect and it should be `true` as Java is case-sensitive.
    2. The print statement in `else` is missing and semicolon (`;`) at the end.
    Now, the fixed code is:
    ``java
    boolean dh = true;
    boolean cr= true;
    if (dh && cr)
    System.out.println("You cannot go out");
    else
    System.out.println("You can go out");
    
    
  4. Sam executes the following program segment and the answer displayed is zero irrespective of any non zero values are given. Name the error. How the program can be modified to get the correct answer?

    void triangle(double b, double h){
        double a;
        a = 1 / 2 * b * h;
        System.out.println(Area=+a);
    }
    

    Answer

    void triangle(double b, double h){
        double a;
        a = 1.0 / 2.0 * b * h;
        System.out.println(Area=+a);
    }
    

    Explanation

    The mathematical expression 1 / 2 * b * h, would be wrong in Java as the parameters b and h are both of type double and the constants 1 and 2 are ints. So, to fix it, we need to change 1 to 1.0 and 2 to 2.0 since the default data type for floating point numbers in Java is double.

  5. How many times will the following loop execute? What value will be returned?

    int x=2;
    int y=50;
    do {
        ++x;
        y -= x++;
    } while(x<=10);
    return y;
    

    Answer

    The loop will run 5 times. The value returned will be 15.

  6. Write the output of the following String methods:

    1. "ARTIFICIAL ".indexOf('I' )
      

      Answer

      3

    2. "DOG and PUPPY". trim().length()
      

      Answer

      13

  7. Name any two jump statements.

    Answer

    1. break
    2. continue

    (return is also a jump statement)

  8. Predict the output of the following code snippet:

    String a="20";
    String b="23";
    int p=Integer.parseInt(a);
    int q=Integer.parseInt(b);
    System.out.print(a+"*"+b);
    

    Answer

    20*23
    
  9. When there is no explicit initialization, what are the default values set for variables in the following cases?

    1. Integer variable — 0

    2. String variable — null

  10. int P [ ]={ 12,14,16,18}; int Q[ ]={ 20,22,24};
    

    Place all elements of P array and Q array in the array R one after the other.

    Answer

    1. Size of R will be length of P + length of Q which is 7.

    2. Index of the first item/element will be 0 and last will be length of R - 1 which is 7 - 1 = 6.

Go to Section-B