Java Question Tags for Class 9

Output-based Questions

  1. Find the final value of “c” in the following statement:
    int a = 12, b = 9, c = -2;
    c += a++ + ++a - --b + --a;

    c = c + a++ + ++a – –b + –a;
    ⇒ c = -2 + 12 + 14 – 8 + 13
    ⇒ c = 10 + 14 – 8 + 13
    ⇒ c = 24 – 8 + 13
    ⇒ c = 16 + 13
    ⇒ c = 29.
  2. Evaluate the value of the variable “z” after the following code snippet executes:
    int z = 7;
    z += z-- - --z - z;

    z = z + z– – –z – z
    ⇒ z = 7 + 7 – 5 – 5
    ⇒ z = 14 – 5 – 5
    ⇒ z = 9 – 5
    ⇒ z = 4
  3. What will be the result stored in “x” after evaluating the following expression?
    int x = 4;
    x += (x++) + (++x) + x;

    x = x + (x++) + (++x) + x
    ⇒ x = 4 + 4 + 6 + 6
    ⇒ x = 8 + 6 + 6
    ⇒ x = 14 + 6
    ⇒ x = 20.
  4. What is the output of the following code segment?
    int x = 10;
    if(x > 10 && x < 30)
        x++;
    else
        x--;
    System.out.print(x);

    OUTPUT: 9

  5. Give the output:
    double number = Math.sqrt(9 / 2);
    double result = Math.pow(2, 3);
    System.out.println("Number = " + number);
    System.out.println("Result = " + result);
    OUTPUT:
    Number = 2.0
    Result = 8.0

Conditional Operator

  1. Give the syntax of conditional operator with an example.
    Syntax:
    <variable> = <boolean expression>? <value 1> : <value 2>;
    Example:
    int max = (a > b)? a : b;
  2. Rewrite the following code snippet using ternary operator:
    String message;
    if(temp >= 20 && temp <= 25)
        message = "Comfortable";
    else
        message = "Uncomfortable";

    String message = (temp >= 20 && temp <= 25)? "Comfortable" : "Uncomfortable";

  3. Rewrite the following code snippet using ternary operator:
    if(marks >= 90)
        grade = "A";
    else
        grade = "B";

    grade = (marks >= 90)? "A" : "B";

Java Expression

  1. Rewrite the following expression as Java expression:
    √((a + b) / 5)
    Math.sqrt((a + b) / 5.0)

Theory Questions

  1. Explain white spaces. What is its use and rules?
    White spaces are the non-printing characters in Java.
    They are used for formatting the output of a Java program.
    The rule is that they must be enclosed in double quotes while printing.
    Example:
    Blank space: ” ”
    Horizontal tab: “\t”
    New line: “\n”
  2. What is the difference between int i and float i?
    int i will only be able to store integers.
    float i will be able to store floating point numbers.
  3. What is casting? Give one example.
    Casting means changing the data type of a value from one type to another. There are two types of type casting: Implicit and Explicit.
    Example:
    char ch = (char)(65); //explicit type casting
    int i = 'A'; //implicit type casting
  4. What is an empty loop? Give one example and one use.
    An empty loop is one that does not contain any statement in its body section.
    Example:
    for(i = 1; i <= 1000; i++);
    It is generally used to create a delay in program execution, or to update the counter/accumulator.
  5. What is an infinite loop? When does it usually occur?
    An infinite loop is one that never stops executing its loop body.
    Example:

    for(int i = 1;i <= 10;){
        System.out.println("Infinite loop");
    }

    It usually occurs when we forget to update the loop counter so that the loop terminates at certain point of execution.

  6. What is a while loop? Give its general form.
    A while loop is an entry-controlled loop in Java. In this loop, the initialization, condition, and increment/decrement are all done in different parts of the loop.
    General form:

    while(<boolean expression>){
        <statement(s)>
    }
  7. Define polymorphism and abstraction.
    Polmosphism is the process of using a function or method for more than one purpose.
    Abstraction is the act of representing the essential features without knowing the background details.
  8. Name any two types of errors that may occur in a computer program.
    Compile-time error, Logical error and Run-time error (any two).
  9. Name two jump statements/keywords used in loops.
    break and continue.

Leave a Reply

%d bloggers like this: