Java Question Tags for Class 8

Syntax Questions

  1. Give the syntax of the if statement with an example.
    Syntax:

     

    if(<condition>){
        <statement(s)>
    }

    Example:

    if(marks >= 40)
        System.out.println("Pass");
  2. Write the syntax/general format of for statement. Give an example.
    Syntax:

     

    for(<initialization>; <boolean expression>; <increment/decrement>){
        <statement(s)>
    }

    Example:

    for(i = 1; i <= 10; i++)
        System.out.println(i);

Counters and Accumulators

  1. Given the three Java statements, state which one among them is a counter and which one an accumulator:
    a. i++; (counter)
    b. a = a + 5; (accumulator)
    c. a += 5; (accumulator)

Output-based Questions

  1. Write the output of the following code in Java:
    int a = 8, b = 18;
    System.out.println("a = " + a + "\tb = " + b);
    System.out.println("Sum + " + (a + b));
    System.out.println("Difference = " + (b - a));

    OUTPUT:

     

    a = 8        b = 18
    Sum + 26
    Difference = 10
  2. Find the output:
    int x = 4, y = 81;
    System.out.println(x > y);
    System.out.println(Math.sqrt(y));
    System.out.println(++x + (y % x));

    OUTPUT:
    false
    9.0
    6
  3. If a = 24 and b = 12, then find and display the values of a, b, and c in the following expression:
    c = a++ - --b;
    a = 25.
    b = 11.
    c = 24 – 11 = 13.
  4. Find the output of the given code:
    int a = 5, b = 2, c = 8;
    b += ++a - b;
    c -= a++ + ++b;
    System.out.print("a = " + a + "\nb = " + b + "\nc = " + c);

    b = b + ++a – b
    ⇒b = 2 + 6 – 2 = 6
    c = c – (a++ + ++b)
    ⇒c = 8 – (6 + 7) = -5
    OUTPUT:
    a = 7
    b = 7
    c = -5
  5. Find the output for the following code:
    int a = 5, b = 3;
    b += ++a + a++;
    System.out.print("a = " + a + "\nb = " + b);

    OUTPUT:
    a = 7
    b = 15

Theory Questions:

  1. Differentiate between if and if-else with example.
    The if statement contains statements only for the true block.

     

    if(marks >= 40)
        System.out.println("Pass");

    The if-else statement contains statements both for true and false block.

    if(marks>= 40)
        System.out.println("Pass");
    else
        System.out.println("Fail");
  2. What are Java Tokens? Name three of them.
    Java tokens are the basic building blocks of a Java program.
    Keywords, Identifiers and Literals are three Java tokens.
  3. What is Java? When was it invented and by whom?
    Java is an object-oriented, platform-independent programming language.
    It was developed in 1991 by James Gosling, but the first version was released in the year 1995.
  4. Why is Java called a platform independent language?
    When a Java program is compiled, it produces bytecode, which is platform independent because it can execute in any platform, having JVM. Thus, Java is called a platform independent language.
  5. What is the difference between variable and keyword?
    Variables are also known as identifiers in Java. They are the named storage locations that hold values.
    Example:
    int x = 5; //x is a variable
    Keywords are special words in Java that have a special meaning to the compiler.
    Example:
    public, static, void.
  6. Differentiate between = and == in Java.
    The = sign is an assignment operator. It is used to assign a value to a variable.
    Example:
    int x = 5;
    The == sign is a relational operator. It is used to compare two values for equality.
    Example:

     

    if(x == y)
        System.out.println("Same");
  7. State the difference between System.out.print() and System.out.println() with an example for each.
    The print() method displays the message and keeps the cursor on the same line.
    Example:
    System.out.print("Hello ");
    System.out.print("World");

    OUTPUT:
    Hello World
    The println() method displays the message and takes the cursor to the next line.
    Example:
    System.out.println("Hello ");
    System.out.println("World");

    OUTPUT:
    Hello
    World
  8. Differentiate between prefix and postfix.
    In prefix, the variable first updates its value, and then assigns it.
    Example:
    int x = 5;
    int y = ++x; //y gets 6

    Whereas in postfix, the variable first assigns its value, then later it updates it.
    Example:
    int x = 5;
    int y = x++; //y gets 5
  9. Differentiate between a counter and an accumulator.
    A counter is a variable that increments/decrements its value by 1 each time. They are mainly used to count a certain operation that repeats in a program.
    Example:
    x++;
    An accumulator is a variable that updates its value, but not by 1. They are mainly used to find the result of a calculation that involves many values, like the total sum, factorial, etc.
    Example:
    x += 5;
  10. State the function of Math.pow() and Math.sqrt(), with an example each.
    Math.pow() function returns the value of the first argument raised to the power of the second argument.
    Example:
    double p = Math.pow(2, 3); //p gets 8.0
    Math.sqrt() function returns the square root of a positive number.
    Example:
    double s = Math.sqrt(49); //s gets 7.0
  11. State any three rules for naming a variable in Java.
    Following are the three rules for naming variables in Java:
    a. Variable names can begin with a letter, underscore or dollar sign.
    b. Variables can’t be the same as the keywords.
    c. Variables are case-sensitive.
  12. Write the symbols and names of the logical operators. What is their order of precedence?
    The three logical operators are Logical AND (&&), Logical OR (||) and Logical NOT (!). The order of precedence is NOT –> AND –> OR.

Conditional Operators

  1. Rewrite without using if:
    if(bill < 7000)
        discount = 0.05;
    else
        discount = 0.1;

    discount = (bill < 7000)? 0.05 : 0.1;

  2. Give the syntax of the ternary operator and also an example.
    Syntax:
    <variable> = (<boolean expression>)? <value 1> : <value 2>;
    Example:
    int max = (a > b)? a : b;
  3. Rewrite the given code without using ternary operator:
    double ans = (value > 10000)? 0.05 * value : 0.02 * value;

     

    double ans;
    if(value > 10000)
        ans = 0.05 * value;
    else
        ans = 0.02 * value;

Debug the Errors

  1. Find and correct the errors (if any):
    Circle class
    {
        public static voidmain(string args[])
        {
            double a = 75.57d, 12.56d, 11.73d, smo = 0.0d
            a + b + c = sum;
            System.out.println("Sum of numbers = " + sum);
        }
    }

    Correct Code:

    class Circle
    {
        public static void main(String args[])
        {
            double a = 75.57d, b = 12.56d, c = 11.73d, sum = 0.0d;
            sum = a + b + c;
            System.out.println("Sum of numbers = " + sum);
        }
    }

Java Expressions:

  1. Write the following expression in one Java statement:
    t = 3 / 7 (a7 + d4)
    t = 3.0 / 7 * (Math.pow(a, 7) + Math.pow(d, 4));

1 thought on “Java Question Tags for Class 8”

Leave a Reply

%d bloggers like this: