Categories
Class 8

Introduction to Java

Fill in the blanks with the correct words:

  1. The int data type takes four bytes of storage.
  2. ‘A’ is a character constant.
  3. float a = 45.34; is a/an initialization statement.
  4. The output of 20 % 6 in Java will be 2.
  5. The do-while is an exit-controlled loop.

State whether True or False. Correct the false statement(s):

  1. In Java, a class is a capsule of data members and member functions. TRUE
  2. To run a Java program, double-click the compiled class. FALSE
    (Right-click the class icon and click void main() to run a Java program.)
  3. A variable name can start with a digit. FALSE
    (A variable name can start with a letter/underscore/dollar character.)
  4. The selection statements in Java are if and switch. TRUE
  5. The while loop is executed at least once. FALSE
    (The do-while loop is executed at least once.)

Choose the correct option:

  1. Which data type is used to store a whole number?
    a) float
    b) int
    c) double
    d) long double
  2. Which of these is an invalid String constant?
    a) “9”
    b) “Hello”
    c) ‘9’
    d) “*****”
  3. In the switch statement, if the value does not match any case label, then:
    a) Statements after break are executed
    b) Statements in the default case are executed
    c) An error occurs
    d) None of these
  4. Which of the following is a conditional statement?
    a) for
    b) while
    c) if
    d) do-while
  5. Which of these is not a valid initialization?
    a) int a = 12;
    b) float b = 34.6
    c) char a = ‘A’;
    d) String s = ‘Hello’;

Answer the following:

  1. What do you understand by object-oriented programming?
    An object-oriented programming is a different and modern approach to programming that is based on the fundamental concept of objects.
  2. What is a class in Java? What is it composed of?
    A class is a collection of similar objects. It is composed of properties and behavior.
  3. What is a method? Explain its general format.
    A method/function is a set of code that can be used at different points in the program. Following is its general format:

     

    <access specifier> <return type> <method name>(formal parameter list)
    {
        //body of the method
    }
  4. What do you understand by data type? List the data types in Java.
    Data types help decide how many bytes a variable will take for storage, and the range of values it can store. Following is the list of data types in Java: byte, short, int, long, float, double, char, boolean.
  5. Name the three looping statements in Java. Write their syntax.
    The three looping statements in Java are: while loop, do-while loop and for loop.
    Syntax of for loop:

     

    for(initialization; condition; increment/update){
        statement(s)
    }

    Syntax of while loop:

    while(condition){
        statement(s)
    }

    Syntax of do-while loop:

    do{
        statement(s)
    }while(condition);

Application-Based Questions

a. What will be suitable data types for the following constant values?
i. 101 = int
ii. “123” = String
iii. ‘A’ = char
iv. 23.4351 = double
v. true = boolean

b. What will be the output of the following method?

public static void main()
{
    int a = 5;
    a++;
    System.out.println(a);
    a = a-- - --a;
    System.out.println(a);
}

OUTPUT:
6
2

c. Find out the invalid identifier  from the list given below:
Rollno1 is Valid.
A12B is Valid.
_A1 is Valid.
1Salary is Invalid.
Emp_Name is Valid.

d. Rewrite the following program segment using the while loop. Then write the complete Java code including the while loop to print the factorial of 5.

int factorial = 1, i;
for(i = 1; i <= 5; i++)
    factorial *= i;
System.out.println("Factorial of 5 = " + factorial);
class Factorial{
    public static void main(String args[]){
        int factorial = 1, i = 1;
        while(i <= 5){
            factorial *= i;
            i++;
        }
        System.out.println("Factorial of 5 = " + factorial);
    }
}

e. Write the following if-else-if program construct using the switch statement. Then, write the complete code in Java including the switch statement to print the correct direction.

char a;
String direction = "";
if(a == 'N')
    direction = "North";
else if(a == 'S')
    direction = "South";
else if(a == 'W')
    direction = "West";
else
    direction = "East";
System.out.println(direction);
class Directions{
    public static void main(String args[]){
        char a = 'N';
        String direction = "";
        switch(a){
            case 'N':
            direction = "North";
            break;
            case 'S':
            direction = "South";
            break;
            case 'W':
            direction = "West";
            break;
            default:
            direction = "East";
        }
        System.out.println(direction);
    }
}

Hands On!

1. Write a program in Java that displays “I want to learn programming in Java” 50 times using the for loop.

class Repeat{
    public static void main(String args[]){
        for(int i = 1; i <= 50; i++)
            System.out.println("I want to learn programming in Java");
    }
}

2. Write a program in Java to print odd numbers from 1 to 100.

class Odd{
    public static void main(String args[]){
        for(int i = 1; i <= 100; i += 2)
            System.out.println(i);
    }
}

3. Write a program in Java to accept a parameter, called total_marks, out of 500. The program should calculate and display the percentage and the grade as per the criteria given in the table alongside.

PercentageGrade
>= 90A
>= 80 and < 90B
>= 70 and < 80C
>= 60 and < 70D
< 60E
class Percentage{
    public static void main(int total_marks){
        double p = total_marks / 500.0 * 100;
        System.out.println("Percentage: " + p);
        char grade = '\u0000';
        if(p >= 90)
            grade = 'A';
        else if(p >= 80)
            grade = 'B';
        else if(p >= 70)
            grade = 'C';
        else if(p >= 60)
            grade = 'D';
        else
            grade = 'E';
        System.out.println("Grade: " + grade);
    }
}

4. Write a program in Java to input any number through a function argument and print the sum of its digits.
Hint: If the number is 1983, the output should be 1 + 9 + 8 + 3 = 21

class Solve{
    public static void main(int num){
        int sum = 0;
        while(num != 0){
            sum += num % 10;
            num /= 10;
        }
        System.out.println("Sum of the digits: " + sum);
    }
}

5. Write a program in Java to accept three integer parameters and print the largest of these numbers.

class Largest{
    public static void main(int a, int b, int c){
        if(a > b){
            if(a > c)
                System.out.println(a);
            else
                System.out.println(c);
        }
        else{
            if(b > c)
                System.out.println(b);
            else
                System.out.println(c);
        }
    }
}

2 replies on “Introduction to Java”

Tomorrow I have exam i have founded any website but i have not get to and at last i foun in this web. Seriously very helpful.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.