Fundamentals of Java Programming Class 12 Questions and Answers

Teachers and Examiners (CBSESkillEduction) collaborated to create the Fundamentals of Java Programming Class 12 Questions and Answers. All the important Information are taken from the NCERT Textbook Information Technology (802) class 12.

Fundamentals of Java Programming Class 12 Questions and Answers

1. What is Variable?
Answer – A variable is a storage location for information whose value may vary while a programme is running. A variable is, technically speaking, the name of a storage area in the computer’s internal memory. The data present there serves as the variable’s value.

2. What are the different Variable naming rules in Java?
Answer – The variable naming rules in Java are –
a. Variable names can begin with either an alphabetic character, an underscore, or a dollar sign.
b. Space is not allowed in variable names.
c. Reserved words are not used in variables.
d. Java is a case-sensitive language.

Fundamentals of Java Programming Class 12 Questions and Answers

3. What are the different Primitive Data Types in Java?
Answer – There are eight different types of primitive data types in Java.
a. Byte
b. Short
c. Int
d. Long
e. Float
f. Double
g. Char
h. Boolean

4. What is String Variable?
Answer – String variables, also known as alphanumeric or character variables, have values that are interpreted as text. In other words, string variables’ values could be made up of letters, numbers, or symbols.

5. What is Operator and what are the different types of Operator?
Answer – Operators are special symbols in a programming language and perform certain specific operations. Java support –
a. Arithmetic Operators : +, -, *, /, %, ++, —
b. Relational Operators : ==, !=, >, <, >=, <=
c. Assignment Operators : =, +=, -=, *=, /=, %=
d. Logical Operators : &&, ||, !

Fundamentals of Java Programming Class 12 Questions and Answers

6. Difference between Entry control loop and Exit control loop.
Answer – Difference between entry control and Exit control loop are –
Entry Control Loop –
a. Entry Control Loop tests the condition first and then executes the body of the loop.
b. If the condition is false, Entry control loop will not execute
c. Example of entry control loop are – for loop and while loop

Exit Control Loop –
a. Exit Control loop tests the condition after running a block of code.
b. If the condition is false, the Entry control loop will execute at least one time.
c. Example of entry control loop are – do-while

7. What is an Array?
Answer – Arrays are variables that can hold more than one value, they can hold a list of values of the same type. Example – marks = new double[5];

Fundamentals of Java Programming Class 12 Questions and Answers

8. What is the purpose of user defined methods?
Answer – User-defined functions are techniques you can use to arrange your code within a body. Once a function has been defined, it can be used in the same way that the built-in action and parser functions are used. Instead of being passed by value, variables are passed by reference to functions.

9. What are OOPs?
Answer – OOPs stands for Object Oriented Programming, Java is an Object Oriented Programming (OOP) language. In an OOP language, a program is a collection of objects that interact with other objects to solve a problem. Each object is an instance of a class.

10. What is the difference between local and global variables?
Answer – Depending on their scope, variables are divided into global variables and local variables. Local variables can only be accessed within the function or block in which they are defined, In other hand the global variables, which can be used worldwide throughout the entire programme.

Fundamentals of Java Programming Class 12 Questions and Answers

11. What is the purpose of Constructor in Java?
Answer – A special method member called the constructor method is used to initialize the data members of the class (or any other initialization is to be done at time of object creation). The constructor has the same name as the class, has no return type, and may or may not have a parameter list. Whenever a new object of a class is created, the constructor of the class is invoked automatically. We do not call the constructor explicitly.

12. What are the different types of Access Modifiers?
Answer – Access modifiers are keywords that manage a class’s fields, methods, and function. Examples of access modifiers are Public, protected, and private.

Fundamentals of Java Programming Class 12 Questions and Answers

13. What is the purpose of Getter and Setter Methods?
Answer – Private data members of a class cannot be accessed outside the class however, you can give controlled access to data members outside the class through getter and setter methods.

Basic Java program –

14. Write a program to accept integer number from user and print the number.

import java.util.Scanner;
public class HelloWorld {

    public static void main(String[] args) {

        Scanner reader = new Scanner(System.in);
        System.out.print("Enter a number: ");

        int number = reader.nextInt();

        System.out.println("You entered: " + number);
    }
}

Output - 
Enter a number: 20
You entered: 20

15. Write a program to accept two numbers from the user and find the sum of numbers.

class Example {

  public static void main(String[] args) {
    
  int n1=22, n2=44, sum;
    
    sum = n1 + n2;
    System.out.println("The sum is: " + sum);
  }
}

Output - 
Enter two numbers
22 44
The sum is: 66

Fundamentals of Java Programming Class 12 Questions and Answers

16. Write a program to swap the numbers without using 3rd variable.

public class Swap {

    public static void main(String[] args) {

        int n1=51, n2=48;
        System.out.println("First number = " + n1);
        System.out.println("Second number = " + n2);
        n1=n1+n2;
        n2=n1-n2;
        n1=n1-n2;

        System.out.println("Result");
        System.out.println("First number = " + n1);
        System.out.println("Second number = " + n2);
    }
}

Output - 
First number = 51
Second number = 48
Result
First number = 48
Second number = 51

Fundamentals of Java Programming Class 12 Questions and Answers

If Statement related program – 

17. Write a program to accept number from the user and check whether number is even or odd.

import java.util.Scanner;
public class Example {
    public static void main(String[] args) {

        Scanner reader = new Scanner(System.in);
        System.out.print("Enter a number: ");
        int num = reader.nextInt();
        if(num % 2 == 0)
            System.out.println(num + " is even");
        else
            System.out.println(num + " is odd");
    }
}

Output - 
Enter a number: 22
22 is even

Fundamentals of Java Programming Class 12 Questions and Answers

18. Write a program to accept number from the user and check whether an alphabet is vowel or consonant.

import java.util.Scanner;
public class Example {
    public static void main(String[] args) {

        Scanner reader = new Scanner(System.in);
        System.out.print("Enter a character: ");
        char ch = reader.next().charAt(0);

        if(ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' )
            System.out.println(ch + " is vowel");
        else
            System.out.println(ch + " is consonant");

    }
}

Output - 
Enter a character: a
a is vowel

19. Write a program to accept three numbers from the user and find the largest number.

public class Example {
    public static void main(String[] args) {

	Scanner reader = new Scanner(System.in);
        System.out.print("Enter a 1st number: ");
        char n1 = reader.nextInt();

        System.out.print("Enter a 2nd number: ");
        char n2 = reader.nextInt();

        System.out.print("Enter a 3rd number: ");
        char n3 = reader.nextInt();

        if( n1 >= n2 && n1 >= n3)
            System.out.println(n1 + " is the largest number.");

        else if (n2 >= n1)
            System.out.println(n2 + " is the largest number.");

        else
            System.out.println(n3 + " is the largest number");
    }
}

Output - 
Enter a 1st number: 7
Enter a 2nd number: 3
Enter a 3rd number: 9
9 is the largest number

Fundamentals of Java Programming Class 12 Questions and Answers

20. Write a program to check leap year.

public class Example {

  public static void main(String[] args) {

    int year = 2022;
    boolean leap = false;

    if (year % 4 == 0) {

      if (year % 100 == 0) {
        if (year % 400 == 0)
          leap = true;
        else
          leap = false;
      }
      
      else
        leap = true;
    }
    
    else
      leap = false;

    if (leap)
      System.out.println(year + " is a leap year.");
    else
      System.out.println(year + " is not a leap year");
  }
}

Output - 
2022 is not a leap year

Fundamentals of Java Programming Class 12 Questions and Answers

21. Write a program the check weather a number is positive or negative.

public class Example {

    public static void main(String[] args) {

        double n1 = 18;

        if (number < 0.0)
            System.out.println(number + " is a negative number");

        else if ( number > 0.0)
            System.out.println(number + " is a positive number");

        else
            System.out.println(number + " is 0");
    }
}

Output - 
18 is a positive number

22. Write a program to check whether a character is alphabet or not.

public class Example {

    public static void main(String[] args) {

        char c = 'a';

        if( (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
            System.out.println(c + " is an alphabet");
        else
            System.out.println(c + " is not an alphabet");
    }
}

Output - 
a is an alphabet

Fundamentals of Java Programming Class 12 Questions and Answers

Looping statement related program – 

23. Write a program to Find Factorial of a Number
n = 1 * 2 * 3 * 4 * … * n

public class Example {

    public static void main(String[] args) {

        int num = 10;
        long f = 1;
        for(int i = 1; i <= num; ++i)
        {
            f = f * i;
        }
        System.out.printf("Factorial of a number ", f);
    }
}

Output - 
Factorial of a number = 3628800

24. Write a program to print the Fibonacci series 0, 1, 1, 2, 3, 5, 8,

class Example {
  public static void main(String[] args) {

    int n = 10, first = 0, second = 1, sum = 0;

    for (int i = 1; i <= n; i++) {
      System.out.print(first + ", ");

      sum = first + second;
      first = second;
      second = sum;
    }
  }
}

Fundamentals of Java Programming Class 12 Questions and Answers

25. Write a program to check whether the string is palindrome or not.

class Example {
  public static void main(String[] args) {

    String str = "MADAM", r_str = "";
    int strLength; 
    
    int strLength = str.length();

    for (int i = (strLength - 1); i >=0; i--) {
      r_str = r_str + str.charAt(i);
    }

    if (str.toLowerCase().equals(r_str.toLowerCase())) {
      System.out.println(str + " is a Palindrome");
    }
    else {
      System.out.println(str + " is not a Palindrome");
    }
  }
}

Output - 
MADAM is a Palindrome

Fundamentals of Java Programming Class 12 Questions and Answers

26. Write a program to check whether a number is prime or not.

public class Example {

  public static void main(String[] args) {

    int num = 7;
    boolean f = false;
    for (int i = 2; i <= num / 2; i++) {
      if (num % i == 0) {
        f = true;
        break;
      }
    }

    if (!f)
      System.out.println(num + " is a prime number");
    else
      System.out.println(num + " is not a prime number");
  }
}

Output - 
7 is a prime number

Switch Statement related program – 

27. Write a program to design simple calculator using switch statement.

import java.util.Scanner;
class Example {
  public static void main(String[] args) {

    char ch;
    Double number1, number2, result;

    Scanner input = new Scanner(System.in);

    System.out.println("Choose an operator: +, -, *, or /");
    System.out.println("1. +");
    System.out.println("2. -");
    System.out.println("3. *");
    System.out.println("4. /");

    System.out.println("Select choice");

    ch = input.next().charAt(0);

    System.out.println("Enter first number");
    number1 = input.nextDouble();
    System.out.println("Enter second number");
    number2 = input.nextDouble();

    switch (ch) {

      case '+':
        result = number1 + number2;
        System.out.println(number1 + " + " + number2 + " = " + result);
        break;

      case '-':
        result = number1 - number2;
        System.out.println(number1 + " - " + number2 + " = " + result);
        break;

      case '*':
        result = number1 * number2;
        System.out.println(number1 + " * " + number2 + " = " + result);
        break;

      case '/':
        result = number1 / number2;
        System.out.println(number1 + " / " + number2 + " = " + result);
        break;

      default:
        System.out.println("Invalid choice!");
        break;
    }

    input.close();
  }
}

Output - 
1. +
2. -
3. *
4. /
Select Choice +
Enter first number 4
Enter second number 6
4 * 6 = 10

Fundamentals of Java Programming Class 12 Questions and Answers

Array

28. Write a program to Print an Array.

public class Example {

    public static void main(String[] args) {
        int[] array = {1, 2, 3, 4, 5};

        for (int element: array) {
            System.out.println(element);
        }
    }
}

Output - 
1
2
3
4
5

29. Write a program to find the average of given array 7, 34, 55, 82, 44.

public class Example {

    public static void main(String[] args) {
        int[] numArray = { 7, 34, 55, 82, 44 };
        double sum = 0;

        for (double num: numArray) {
           sum += num;
        }

        double average = sum / numArray.length;
        System.out.format("The average is " + average);
    }
}

Output - 
The average is: 44.4

String function related program –

30. Write a program to find the concatenate of two different string

public class Example {
    public static void main(String[] args) {
    String str1="Anurag";
    String str2="Anand"; 
    
    String str3=str1.concat(str2);
    
    System.out.println(str3);
    
    }
}

Output - 
Anurag Anand

31. Write a program to find the length of the string

public class Example {
    public static void main(String[] args) {
    String str="Anurag";
    
    int len=str.length();
    
    System.out.println(len);
    
    }
}

Output - 
6

32. Write a program to convert the string in uppercase format.

public class Example {
    public static void main(String[] args) {
    String str="anurag";
    
    String upper=str.toUpperCase();
    
    System.out.println(upper);
    
    }
}

Output - 
ANURAG

33. Write a program to convert the lowercase string to uppercase string.

public class Example {
    public static void main(String[] args) {
    String str="ANURAG";
    
    String lower=str.toLowerCase();
    
    System.out.println(lower);
    
    }
}

Output - 
anurag

Employability Skills Class 12 Notes

Employability Skills Class 12 MCQ

Employability Skills Class 12 Questions and Answers

Information Technology Class 12 802 Notes

Information Technology Class 12 802 MCQ

Information Technology Class 12 802 Questions and Answers

error: Content is protected !!