0% found this document useful (0 votes)
20 views

Java File

The document contains 10 questions about Java programming concepts and the answers provided code snippets to solve each question. The questions cover topics like command line arguments, data types, operators, conditional statements, loops, methods and more. Full Java programs are provided as solutions for each question.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Java File

The document contains 10 questions about Java programming concepts and the answers provided code snippets to solve each question. The questions cover topics like command line arguments, data types, operators, conditional statements, loops, methods and more. Full Java programs are provided as solutions for each question.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Q-1 Write a program in java which prints your name using command line arguments.

Ans-

class Name {

public static void main(String args[]) {

if (args.length > 0) {

System.out.println("Your name is: " + args[0]);

else {

System.out.println("Please provide your name as a command line argument.");

OUTPUT

Q-2 Write a program in java which enters three numbers using command line argument and print sum
and average of the number.
Ans-

class Main {

public static void main(String args[]) {

int a = Integer.parseInt(args[0]);

int b = Integer.parseInt(args[1]);

int c = Integer.parseInt(args[2]);

int sum = a + b + c;

double avg =(double) sum / 3.0;

System.out.println("Sum of the numbers: " + sum);


System.out.println("Average of the numbers: " + avg);

}
OUTPUT

Q-3 Write a program to swap the value of 2 variable without using 3rd variable.
Ans-

class SwapWithoutThirdVariable {

public static void main(String args[]) {

int a = 10;

int b = 20;

System.out.println("Before swapping: a = " + a + ", b = " + b);

a = a + b;

b = a – b;

a = a - b;

System.out.println("After swapping: a = " + a + ", b = " + b);

}
OUTPUT
Q-4 Write a program to calculate the sum of digits of given interger number.
Ans-

import java.util.Scanner;

class SumOfDigits {

public static void main (String args[]) {

Scanner sc = new Scanner (System.in) ;

System.out.print("Enter an integer: ");

int number = sc.nextInt();

int sum = 0;

while (number > 0) {

sum += number % 10;

number /= 10;

System.out.println("Sum of digits: " + sum);

}
OUTPUT

Q-5 Write a program to compute the sum of the first and last digit of a given number.
Ans-

import java.util.Scanner;

class FirstAndLastDigitSum {

public static int sumFirstAndLastDigit(int number) {

if (number < 0) {

return -1;

int L = number % 10;

int a = number;
while (a >= 10) {

a /= 10;

return a + L;

public static void main(String args[]) {

Scanner sc = new Scanner(System.in);

System.out.print("Enter a number: ");

int input = sc.nextInt();

int result = sumFirstAndLastDigit(input);

if (result == -1) {

System.out.println("Invalid input.");

} else {

System.out.println("The sum of the first and last digit is: " + result);

sc.close();

}
OUTPUT

Q-6 Write a program in java which enter the number using data input stream and check whether the
entered number is even or odd.
Ans-

import java.io.DataInputStream;

import java.io.IOException;

class EvenOrOdd {

public static void main(String args[]) {

DataInputStream dis = new DataInputStream(System.in);


int n = 0;

try {

System.out.print("Enter a number: ");

n = Integer.parseInt(dis.readLine());

} catch (IOException | NumberFormatException e) {

System.out.println("Invalid input.");

if (n % 2 == 0) {

System.out.println(n + " is even.");

} else {

System.out.println(n + " is odd.");

}
OUTPUT

Q-7 Write an application that read a string and determine whether it is a palindrome.
Ans-

import java.util.Scanner;

class PalindromeChecker {

public static boolean isPalindrome(String s) {

s = s.toLowerCase();

s = s.replaceAll("[^a-z0-9]", "");

int n = s.length();

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

if (s.charAt(i) != s.charAt(n - i - 1)) {

return false;
}

return true;

public static void main(String args[]) {

Scanner sc = new Scanner(System.in);

System.out.print("Enter a string: ");

String input = sc.nextLine();

if (isPalindrome(input)) {

System.out.println("The string is a palindrome.");

} else {

System.out.println("The string is not a palindrome.");

sc.close();

OUTPUT

Q-8 Write a program to enter a sentence from keyboard and also find all the words in the sentence
with starting character as vowel.
Ans-

import java.util.Scanner;

class VowelWords {

public static void main(String args[]) {

Scanner sc = new Scanner(System.in);

System.out.print("Enter a sentence: ");

String sentence = sc.nextLine();


String[] words = sentence.split(" ");

System.out.println("Words starting with vowels:");

for (String word : words) {

char firstChar = word.charAt(0);

if (firstChar == 'A' || firstChar == 'E' || firstChar == 'I' || firstChar == 'O' || firstChar


== 'U'|| firstChar == 'a'|| firstChar == 'e'|| firstChar == 'i'|| firstChar =='o' || firstChar
== 'u') {

System.out.println(word);

}
OUTPUT

Q-9 Write a program in java which creates the array of size 5. Find the sum and average of the five
numbers.
Ans-

class ArraySumAndAverage {

public static void main(String args[]) {

int[] numbers = new int[5];

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

numbers[i] = (int)(Math.random() * 100);

int sum = 0;

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


sum += numbers[i];

double avg = (double)sum / 5;

System.out.println("The numbers in the array are:");

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

System.out.print(numbers[i] + " ");

System.out.println();

System.out.println("Sum of the numbers: " + sum);

System.out.println("Average of the numbers: " + avg);

}
OUTPUT

Q-10 Create a java that has three version of add method which can Add two, three and four integers.
Ans-

class Addition {

public static int add(int a, int b) {

return a + b;

public static int add(int a, int b, int c) {

return a + b + c;

public static int add(int a, int b, int c, int d) {


return a + b + c + d;

public static void main(String args[]) {

System.out.println("Sum of two integers: " + add(1, 2));

System.out.println("Sum of three integers: " + add(1, 2, 3));

System.out.println("Sum of four integers: " + add(1, 2, 3, 4));

OUTPUT

You might also like