Software Development Exam  >  Software Development Notes  >  Basics of Java  >  Assignment: Java Basics

Assignment: Java Basics | Basics of Java - Software Development PDF Download

Multiple Choice Questions (MCQs)


Q.1. Which of the following is not a valid Java data type?
(a) int
(b) float
(c) string
(d) boolean

Ans. (c)

Q.2. What is the correct syntax to declare a variable in Java?
(a) int x;
(b) variable x;
(c) x = int;
(d) declare int x;

Ans. (a)

Q.3. Which of the following is a valid Java identifier?
(a) 2variable
(b) my_variable
(c) #variable
(d) variable&name

Ans. (b)

Q.4. What is the output of the following code snippet?

int x = 5;

int y = 2;

System.out.println(x / y);

(a) 2
(b) 2.5
(c) 3
(d) 2.0

Ans. (c)

Q.5. Which operator is used for concatenating strings in Java?
(a) +
(b) *
(c) /
(d) -

Ans. (a)

High Order Thinking Questions (HOTS)


Q.1. Write a Java code snippet to swap the values of two variables without using a temporary variable.

int a = 5;

int b = 10;

a = a + b;

b = a - b;

a = a - b;

Q.2. Explain the difference between the prefix and postfix increment operators in Java with an example.

The prefix increment operator (++x) increments the value of x and then returns the new value, while the postfix increment operator (x++) returns the current value of x and then increments it.

Q.3. Write a Java code snippet to calculate the factorial of a given number using a recursive function.

public class Factorial {

    public static int calculateFactorial(int n) {

        if (n == 0)

            return 1;

        else

            return n * calculateFactorial(n - 1);

    }


    public static void main(String[] args) {

        int number = 5;

        int factorial = calculateFactorial(number);

        System.out.println("Factorial of " + number + " is: " + factorial);

    }

}

Q.4. Explain the concept of type conversion in Java and provide an example.

Type conversion in Java is the process of converting one data type to another.
For example:

int x = 5;

double y = x; // Implicit type conversion (widening conversion)

Q.5. Write a Java code snippet to determine whether a given year is a leap year or not.

public class LeapYear {

    public static void main(String[] args) {

        int year = 2023;

        boolean isLeapYear = false;


        if (year % 4 == 0) {

            if (year % 100 == 0) {

                if (year % 400 == 0)

                    isLeapYear = true;

            } else {

                isLeapYear = true;

            }

        }


        if (isLeapYear)

            System.out.println(year + " is a leap year.");

        else

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

    }

}

Fill in the Blanks


Q.1. The process of combining two or more strings in Java is known as ____________.

Ans. concatenation

Q.2. A variable that is declared inside a method is called a ____________ variable.

Ans. local

Q.3. The data type used to represent a single character in Java is ____________.

Ans. char

Q.4. Java supports ____________ type conversion between compatible data types.

Ans. implicit

Q.5. The ____________ operator in Java is used to assign a value to a variable.

Ans. = (equal sign)

True or False


1. Java is a platform-independent programming language. (True/False)

Ans. True

2. The '==' operator in Java compares the values of two variables. (True/False)

Ans. True

3. A switch statement can be used to replace multiple if-else statements in Java. (True/False)

Ans. True

4. In Java, the '&&' operator returns true if both operands are true. (True/False)

Ans. True

5. Java supports automatic type conversion from double to int. (True/False)

Ans. False

Hands-On Questions


Q.1. Write a Java code snippet to calculate the area of a circle with a given radius. (Use the value of π as 3.14)

import java.util.Scanner;


public class CircleArea {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter the radius of the circle: ");

        double radius = scanner.nextDouble();

        double area = 3.14 * radius * radius;

        System.out.println("The area of the circle is: " + area);

    }

}

Q.2. Write a Java code snippet to print the Fibonacci series up to the nth term.

import java.util.Scanner;


public class FibonacciSeries {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter the value of n: ");

        int n = scanner.nextInt();

        int firstTerm = 0, secondTerm = 1;

        System.out.print("Fibonacci Series up to " + n + " terms: ");


        for (int i = 1; i <= n; i++) {

            System.out.print(firstTerm + " ");

            int nextTerm = firstTerm + secondTerm;

            firstTerm = secondTerm;

            secondTerm = nextTerm;

        }

    }

}

Q.3. Write a Java program to find the largest element in an array.

public class LargestElement {

    public static void main(String[] args) {

        int[] numbers = {10, 5, 20, 8, 15};

        int largest = numbers[0];


        for (int i = 1; i < numbers.length; i++) {

            if (numbers[i] > largest) {

                largest = numbers[i];

            }

        }


        System.out.println("The largest element is: " + largest);

    }

}

Q.4. Write a Java code snippet to reverse a string without using any built-in functions or methods.

public class ReverseString {

    public static void main(String[] args) {

        String originalString = "Hello, World!";

        char[] chars = originalString.toCharArray();

        int length = chars.length;

        char[] reversedChars = new char[length];


        for (int i = 0; i < length; i++) {

            reversedChars[i] = chars[length - 1 - i];

        }


        String reversedString = new String(reversedChars);

        System.out.println("Reversed string: " + reversedString);

    }

}

Q.5. Write a Java code snippet to check whether a given string is a palindrome or not.

public class Palindrome {

    public static void main(String[] args) {

        String originalString = "level";

        int length = originalString.length();

        boolean isPalindrome = true;


        for (int i = 0; i < length / 2; i++) {

            if (originalString.charAt(i) != originalString.charAt(length - 1 - i)) {

                isPalindrome = false;

                break;

            }

        }


        if (isPalindrome)

            System.out.println(originalString + " is a palindrome.");

        else

            System.out.println(originalString + " is not a palindrome.");

    }

}

The document Assignment: Java Basics | Basics of Java - Software Development is a part of the Software Development Course Basics of Java.
All you need of Software Development at this link: Software Development
60 videos|37 docs|12 tests

Top Courses for Software Development

60 videos|37 docs|12 tests
Download as PDF
Explore Courses for Software Development exam

Top Courses for Software Development

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

Assignment: Java Basics | Basics of Java - Software Development

,

Free

,

Exam

,

Viva Questions

,

Previous Year Questions with Solutions

,

video lectures

,

ppt

,

mock tests for examination

,

pdf

,

Assignment: Java Basics | Basics of Java - Software Development

,

Summary

,

MCQs

,

Sample Paper

,

Important questions

,

study material

,

practice quizzes

,

Extra Questions

,

Assignment: Java Basics | Basics of Java - Software Development

,

Semester Notes

,

shortcuts and tricks

,

past year papers

,

Objective type Questions

;