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

Exercitii Java

The document provides 10 examples of Java programs to perform various operations on arrays and strings: 1) Print the contents of a 2D Boolean array. 2) Print an array after changing the rows and columns. 3) Find the k largest elements in an array. 4) Find the k smallest elements in an array. 5) Find the kth smallest and largest element in an array. 6) Find numbers greater than the average in an array. 7) Move all positive/negative numbers to opposite sides of an array. 8) Check if a string contains another string. 9) Print the number of prime numbers less than or equal to a given integer. 10) Replace instances

Uploaded by

mihaela.vlad27
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Exercitii Java

The document provides 10 examples of Java programs to perform various operations on arrays and strings: 1) Print the contents of a 2D Boolean array. 2) Print an array after changing the rows and columns. 3) Find the k largest elements in an array. 4) Find the k smallest elements in an array. 5) Find the kth smallest and largest element in an array. 6) Find numbers greater than the average in an array. 7) Move all positive/negative numbers to opposite sides of an array. 8) Check if a string contains another string. 9) Print the number of prime numbers less than or equal to a given integer. 10) Replace instances

Uploaded by

mihaela.vlad27
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 3

1.

Write a Java program to print the contents of a two-dimensional Boolean array where t will
represent true and f will represent false.

Sample array:

array = {{true, false, true},

{false, true, false}};

Expected Output :

tft

ftf

2. Write a Java program to print an array after changing the rows and columns of a given two-
dimensional array.

Original Array:

10 20 30

40 50 60

After changing the rows and columns of the said array:

10 40

20 50

30 60

3. Write a Java program to find the k largest elements in a given array. Elements in the array can
be in any order.

Expected Output:

Original Array:

[1, 4, 17, 7, 25, 3, 100]

3 largest elements of the said array are:

100 25 17

4. Write a Java program to find the k smallest elements in a given array. Elements in the array can
be in any order.
Expected Output:

Original Array:

[1, 4, 17, 7, 25, 3, 100]

3 largest elements of the said array are:

100 25 17

5. Write a Java program to find the kth smallest and largest element in a given array. Elements in
the array can be in any order.

Expected Output:

Original Array:

[1, 4, 17, 7, 25, 3, 100]

K'th smallest element of the said array:

K'th largest element of the said array:

25

6. Write a Java program to find the numbers greater than the average of the numbers of a given
array.

Expected Output:

Original Array:

[1, 4, 17, 7, 25, 3, 100]

The average of the said array is: 22.0

The numbers in the said array that are greater than the average are:

25

100

7. Write a Java program to move every positive number to the right and every negative number to
the left of a given array of integers.

Expected Output:

Original array: [-2, 3, 4, -1, -3, 1, 2, -4, 0]

Result: [-4, -3, -2, -1, 0, 1, 2, 3, 4]


8. Write a Java program to accept two string and test if the second string contains the first one.

Input first string: Once in a blue moon

Input second string: See eye to eye

If the second string contains the first one? False

9. Write a Java program to print the number of prime numbers which are less than or equal to a
given integer.

Input:

n (1 ≤ n ≤ 999,999)

Expected Output:

Input the number(n):

1235

Number of prime numbers which are less than or equal to n.:

202

10. Write a Java program to replace a string "python" with "java" and "java" with "python" in a
given string.

Input:

English letters (including single byte alphanumeric characters, blanks, symbols) are given
on one line. The length of the input character string is 1000 or less.

Output:

Exchanged character string of python and java on one line.

Expected Output:

Input the string:

python is more propular than java

New string:

java is more propular than python

You might also like