Java Basics
Data Types, Variables, Operators,
Control Statements (if/switch)
Data Types in Java
• Java has two categories of data types:
• • Primitive: byte, short, int, long, float, double,
char, boolean
• Example: int age = 25; char grade = 'A';
• • Non-Primitive: Arrays, Strings, Classes,
Interfaces
• Example: String name = "Ashish";
Variables in Java
• Variables store data for processing.
• • Syntax: dataType variableName = value;
• Example: int count = 10;
• • Types: Local, Instance, Static
Constants and final Keyword
• The 'final' keyword makes variables constant.
• • Syntax: final dataType varName = value;
• Example: final double PI = 3.1416;
Operators in Java
• Java provides different types of operators:
• • Arithmetic: +, -, *, /, %
• • Relational: ==, !=, >, <, >=, <=
• • Logical: &&, ||, !
• • Assignment: =, +=, -=, *=, /=
• • Unary: +, -, ++, --
Control Statements: if & if-else
• Used for decision-making:
• • Syntax:
• if (condition) {
• // code
• } else {
• // code
• }
• • Example:
• int age = 18;
Switch Statement
• Use switch for multiple condition checks:
• • Syntax:
• switch(expression) {
• case value: // code; break;
• ...
• }
• • Example:
• int day = 2;
• switch(day) {
Sample Program: Grade Calculator
• Example:
• int marks = 85;
• if (marks >= 90) {
• System.out.println("A");
• } else if (marks >= 80) {
• System.out.println("B");
• }
Sample Program: Calculator using
switch
• Example:
• int a = 10, b = 5;
• char op = '+';
• switch(op) {
• case '+': System.out.println(a+b); break; ... }
Practice Questions
• 1. Write a program to check if a number is
positive or negative.
• 2. Create a calculator using switch case.
• 3. Write a program to print grade based on
marks.
Summary
• • Java data types: Primitive and Non-Primitive
• • Declaring and using variables
• • Operators and their types
• • if, if-else, and switch-case statements
• • Sample problems and questions
Q1: Check Even or Odd
• Problem: Write a Java program to check if a
number is even or odd.
• Solution:
• int num = 10;
• if (num % 2 == 0) {
• System.out.println("Even");
• } else {
• System.out.println("Odd");
Q2: Find Maximum of 3 Numbers
• Problem: Write a Java program to find the
largest of three numbers.
• Solution:
• int a = 5, b = 10, c = 8;
• int max = a;
• if (b > max) max = b;
• if (c > max) max = c;
• System.out.println("Max: " + max);
Q3: Simple Calculator
• Problem: Perform addition, subtraction,
multiplication, and division using switch.
• Solution:
• int a = 20, b = 4;
• char op = '/';
• switch(op) {
• case '+': System.out.println(a + b); break;
• case '-': System.out.println(a - b); break;
Q4: Grading System
• Problem: Print grade based on marks.
• Solution:
• int marks = 72;
• if (marks >= 90) System.out.println("Grade A");
• else if (marks >= 80)
System.out.println("Grade B");
• else if (marks >= 70)
System.out.println("Grade C");
Q5: Positive, Negative, or Zero
• Problem: Check whether a number is positive,
negative, or zero.
• Solution:
• int num = -5;
• if (num > 0) System.out.println("Positive");
• else if (num < 0)
System.out.println("Negative");
• else System.out.println("Zero");

Java_Class_1_Basics_Questions_Solutions (1).pptx

  • 1.
    Java Basics Data Types,Variables, Operators, Control Statements (if/switch)
  • 2.
    Data Types inJava • Java has two categories of data types: • • Primitive: byte, short, int, long, float, double, char, boolean • Example: int age = 25; char grade = 'A'; • • Non-Primitive: Arrays, Strings, Classes, Interfaces • Example: String name = "Ashish";
  • 3.
    Variables in Java •Variables store data for processing. • • Syntax: dataType variableName = value; • Example: int count = 10; • • Types: Local, Instance, Static
  • 4.
    Constants and finalKeyword • The 'final' keyword makes variables constant. • • Syntax: final dataType varName = value; • Example: final double PI = 3.1416;
  • 5.
    Operators in Java •Java provides different types of operators: • • Arithmetic: +, -, *, /, % • • Relational: ==, !=, >, <, >=, <= • • Logical: &&, ||, ! • • Assignment: =, +=, -=, *=, /= • • Unary: +, -, ++, --
  • 6.
    Control Statements: if& if-else • Used for decision-making: • • Syntax: • if (condition) { • // code • } else { • // code • } • • Example: • int age = 18;
  • 7.
    Switch Statement • Useswitch for multiple condition checks: • • Syntax: • switch(expression) { • case value: // code; break; • ... • } • • Example: • int day = 2; • switch(day) {
  • 8.
    Sample Program: GradeCalculator • Example: • int marks = 85; • if (marks >= 90) { • System.out.println("A"); • } else if (marks >= 80) { • System.out.println("B"); • }
  • 9.
    Sample Program: Calculatorusing switch • Example: • int a = 10, b = 5; • char op = '+'; • switch(op) { • case '+': System.out.println(a+b); break; ... }
  • 10.
    Practice Questions • 1.Write a program to check if a number is positive or negative. • 2. Create a calculator using switch case. • 3. Write a program to print grade based on marks.
  • 11.
    Summary • • Javadata types: Primitive and Non-Primitive • • Declaring and using variables • • Operators and their types • • if, if-else, and switch-case statements • • Sample problems and questions
  • 12.
    Q1: Check Evenor Odd • Problem: Write a Java program to check if a number is even or odd. • Solution: • int num = 10; • if (num % 2 == 0) { • System.out.println("Even"); • } else { • System.out.println("Odd");
  • 13.
    Q2: Find Maximumof 3 Numbers • Problem: Write a Java program to find the largest of three numbers. • Solution: • int a = 5, b = 10, c = 8; • int max = a; • if (b > max) max = b; • if (c > max) max = c; • System.out.println("Max: " + max);
  • 14.
    Q3: Simple Calculator •Problem: Perform addition, subtraction, multiplication, and division using switch. • Solution: • int a = 20, b = 4; • char op = '/'; • switch(op) { • case '+': System.out.println(a + b); break; • case '-': System.out.println(a - b); break;
  • 15.
    Q4: Grading System •Problem: Print grade based on marks. • Solution: • int marks = 72; • if (marks >= 90) System.out.println("Grade A"); • else if (marks >= 80) System.out.println("Grade B"); • else if (marks >= 70) System.out.println("Grade C");
  • 16.
    Q5: Positive, Negative,or Zero • Problem: Check whether a number is positive, negative, or zero. • Solution: • int num = -5; • if (num > 0) System.out.println("Positive"); • else if (num < 0) System.out.println("Negative"); • else System.out.println("Zero");