Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
46 views

Java Worksheet1

This document contains 12 problems involving Java programming concepts such as: 1) Reading input and determining the number of negative, positive, and zero values 2) Finding the maximum difference between two elements in an array 3) Checking if a 5-digit integer is a palindrome 4) Using a for loop to compute a summation 5) Checking if an array contains only one digit 6) Finding the second most frequent character in a string 7) Adding two matrices 8) Evaluating the output of a code snippet involving operators 9) Displaying an array in descending order 10) Reading a string and outputting it in uppercase and lowercase 11) Checking if a string ends with the contents of another

Uploaded by

Abdulwahid
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views

Java Worksheet1

This document contains 12 problems involving Java programming concepts such as: 1) Reading input and determining the number of negative, positive, and zero values 2) Finding the maximum difference between two elements in an array 3) Checking if a 5-digit integer is a palindrome 4) Using a for loop to compute a summation 5) Checking if an array contains only one digit 6) Finding the second most frequent character in a string 7) Adding two matrices 8) Evaluating the output of a code snippet involving operators 9) Displaying an array in descending order 10) Reading a string and outputting it in uppercase and lowercase 11) Checking if a string ends with the contents of another

Uploaded by

Abdulwahid
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Worksheet -1

1. (Negative, Positive and Zero Values) Write a java program that inputs five numbers and
determines and prints the number of negative numbers input, the number of positive numbers
input and the number of zeros input.
2. Write a Java program to find maximum difference between two elements in a given array of
integers such that smaller element appears before larger element.

Example:
Input :
nums = { 2, 3, 1, 7, 9, 5, 11, 3, 5 }
Output:
The maximum difference between two elements of the said array elements
10

3. (Palindromes) A palindrome is a sequence of characters that reads the same backward as forward.
For example, each of the following five-digit integers is a palindrome: 12321, 55555, 45554 and
11611. Write an application that reads in a five-digit integer and determines whether it’s a
palindrome. If the number is not five digits long, display an error message and allow the user to
enter a new value.

4. Write a Java program using for loop that can compute the following summation:

5. An array is called vanilla if all its elements are made up of the same digit. For example {1, 1, 11,
1111, 1111111} is a vanilla array because all its elements use only the digit 1. However, the array
{11, 101, 1111, 11111} is not a vanilla array because its elements use the digits 0 and 1. Write a java
program that display true if the array a vanilla array. Otherwise false.

6. Write a Java program to find the second most frequent character in a given

string Sample output


The given string is: successes
The second most frequent char in the string is: c

7. Write a java program that to Add Two Matrices.


8. What will be the output of the following code?
int k = 5, j = 9;
k += k++ – ++j + k;
boolean result= k + j * j > j * ( K + J)-1;
System.out.println(“k=” +k);
System.out.println(“j=” +j);
System.out.println(result);

9. Suppose that scores is an array of 10 components of type double, scores = {2.5, 3.9, 4.8, 6.2, 6.2,
7.4, 7.9, 8.5, 8.5, 9.9} and Display the array element in decreasing order.

10. Write a Java program that reads a string from the keyboard, and outputs the string twice in a
row, first all uppercase and next all lowercase. If, for instance, the string “Hello" is given, the
output will be “HELLOhello".
11. Write a Java program to check whether a given string ends with the contents of another string.

Sample Output:

"java Exercises" ends with "se"? false


"java Exercise" ends with "se"? true

12. Convert the following switch statement into if-else if statements:

String dayString1, dayString2, dayString3;


int day = KB.nextInt();
switch (day) {
case 1: dayString1 = "Saturday";
case 2: dayString2 = "Sunday";
break;
case 3: dayString3 = "Monday";
break;
case 4: dayString1 = "Tuesday";
case 5: dayString2 = "Wednesday";
break;
default: dayString3 = "Invalid day";
break;
}

You might also like