Core9X
Log inSign up

MBOSE Class X Computer Science · Chapter 12

Java Programming

Live now

Learn to write Java programs that take input, do calculations, make decisions and repeat steps with loops.

19 lessons · 110 practice questions

Start freeLog inSign up free: the first chapter of every subject is open.

What this chapter covers

  • Primitive and non-primitive data types
  • Variables and constants
  • Operators and compound assignment
  • String joining with +
  • Steps to evaluate expressions
  • Input with Scanner
  • Output with print and println
  • Sequence, selection and repetition
  • If, switch
  • For, while and do-while loops
  • Standard programs
  • Output and debugging questions

Key terms

primitive type

Meaning: one of the 8 built-in types

Example: int

non-primitive type

Meaning: reference type

Example: String, arrays

reference

Meaning: the address of an object

null

Meaning: a reference to nothing

Example: String s = null;

Unicode

Meaning: character code covering all scripts

Example: char

final

Meaning: keyword for a constant

Example: final int MAX = 100;

concatenation

Meaning: joining strings with +

Example: "a" + "b"

compound assignment

Meaning: operator + =

Example: +=

Scanner

Meaning: class for keyboard input

Example: new Scanner(System.in)

nextInt()

Meaning: reads an int

Example: sc.nextInt()

println

Meaning: prints and moves to a new line

construct

Meaning: basic structure of a program

Example: selection

Important questions with answers

10 of the 110 questions on this chapter, solved.

  1. Question 1 · Multiple choice · 1 mark

    The char data type in Java stores characters in the range

    • (a) 0 to 256
    • (b) –128 to 127
    • (c) 0 to 65535
    • (d) 0 to 32767

    Answer: (c) 0 to 65535

    MBOSE SSLC 2024, Q1(h): 2 bytes, Unicode.

  2. Question 2 · Multiple choice · 1 mark

    Which statement declares an int and gives it a value together?

    • (a) marks = 85;
    • (b) int marks;
    • (c) int marks = 85;
    • (d) int = 85;

    Answer: (c) int marks = 85;

    Declaration and initialisation together.

  3. Question 3 · Multiple choice · 1 mark

    The value of 'A' + 1 in Java is

    • (a) 65
    • (b) B
    • (c) A1
    • (d) 66

    Answer: (d) 66

    The char is used as its code, 65.

  4. Question 4 · Multiple choice · 1 mark

    In for (int i = 1; i <= 10; i++), the initialisation runs

    • (a) once
    • (b) ten times
    • (c) eleven times
    • (d) never

    Answer: (a) once

    SSLC 2023, Q5(c).

  5. Question 5 · Assertion and reason · 1 mark

    Assertion (A): char in Java takes 2 bytes.

    Reason (R): Java uses Unicode for characters.

    • (a) Both A and R are true, and R is the correct explanation of A.
    • (b) Both A and R are true, but R is not the correct explanation of A.
    • (c) A is true, but R is false.
    • (d) A is false, but R is true.

    Answer: (a) Both A and R are true, and R is the correct explanation of A.

    Range 0 to 65,535.

  6. Question 6 · Short answer · 2 marks

    Define data type. Name two primitive data types of Java.

    Answer

    The kind of value a variable holds and its memory size; e.g. int and char.

  7. Question 7 · Short answer · 2 marks

    How do you declare a constant in Java?

    Answer

    With final: final double PI = 3.14159;.

  8. Question 8 · Short answer · 3 marks

    Write the output of: char c = 'A'; for (int i = 1; i <= 3; i++) { System.out.print(c); c++; }

    Answer

    ABC — c moves to the next letter each pass.

  9. Question 9 · Long answer · 5 marks

    Write a Java program to check whether a number is even or odd.

    Answer

    import java.util.Scanner;
    public class EvenOdd
    {
        public static void main(String[] args)
        {
            Scanner sc = new Scanner(System.in);
            System.out.print("Enter a number: ");
            int n = sc.nextInt();
            if (n % 2 == 0)
                System.out.println(n + " is even");
            else
                System.out.println(n + " is odd");
        }
    }
    
  10. Question 10 · Long answer · 5 marks

    Write a Java program to accept marks in three subjects and print the total and percentage.

    Answer

    import java.util.Scanner;
    public class Marks
    {
        public static void main(String[] args)
        {
            Scanner sc = new Scanner(System.in);
            int m1 = sc.nextInt(), m2 = sc.nextInt(), m3 = sc.nextInt();
            int total = m1 + m2 + m3;
            double per = total * 100.0 / 300;
            System.out.println("Total = " + total);
            System.out.println("Percentage = " + per);
        }
    }
    

Study the whole chapter

19 step-by-step lessons, all 110 practice questions with hints, flashcards and mock tests for MBOSE Class X Computer Science.

Start freeLog inSign up free: the first chapter of every subject is open.