0% found this document useful (0 votes)
21 views11 pages

Prashant Oops Quiz

Uploaded by

pranay sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views11 pages

Prashant Oops Quiz

Uploaded by

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

Anushree Mishra

2204921540033
Section : A1 (18)

OOPS QUIZ (Internal


Assessment)
Question 1) Sort given matrix
import java.util.Arrays;

class Anushree {

static void sortMat(int mat[][], int n) {


int[] temp = new int[n * n];
int k = 0;

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


for (int j = 0; j < n; j++) {
temp[k++] = mat[i][j];
}
}

Arrays.sort(temp);
k = 0;

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


for (int j = 0; j < n; j++) {
mat[i][j] = temp[k++];
}
}
}

public static void main(String[] args) {


int n = 3;
int[][] matrix = {
{5, 4, 7},
{1, 3, 8},
{2, 9, 6}
};

System.out.println("Original Matrix:");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}

sortMat(matrix, n);

System.out.println("Sorted Matrix:");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
}
}

Input : mat[][] = { {5, 4, 7}, {1, 3, 8}, {2, 9, 6} }

Output: 1 2 3 4 5 6 7 8 9
Question 2) Find Second largest element from array ?
public class Anushree {

static int findSecondLargest(int[] arr) {


int firstLargest = Integer.MIN_VALUE;
int secondLargest = Integer.MIN_VALUE;

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


if (arr[i] > firstLargest) {
secondLargest = firstLargest;
firstLargest = arr[i];
} else if (arr[i] > secondLargest && arr[i] !=
firstLargest) {
secondLargest = arr[i];
}
}

return secondLargest;
}

public static void main(String[] args) {


int[] arr = {12, 35, 1, 10, 34, 1};

int secondLargest = findSecondLargest(arr);

System.out.println("Second Largest element is: " +


secondLargest);
}
}

Input: arr[] = {12, 35, 1, 10, 34, 1}

Output : Second largest element is 34


Question 3) Move all zeroes to end of array.
public class Anushree {

static void moveZeroesToEnd(int[] arr) {


int n = arr.length;
int count = 0;

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


if (arr[i] != 0) {
arr[count++] = arr[i];
}
}

while (count < n) {


arr[count++] = 0;
}
}

public static void main(String[] args) {


int[] arr = {1, 2, 0, 4, 3, 0, 5, 0};

moveZeroesToEnd(arr);

System.out.println("Array after moving zeroes to the end:");


for (int i : arr) {
System.out.print(i + " ");
}
}
}

Input : arr[] = {1, 2, 0, 4, 3, 0, 5, 0};

Output : arr[] = {1, 2, 4, 3, 5, 0, 0, 0};


Question 4) Find out duplicate fruits.
import java.util.HashMap;
import java.util.Map;

public class Anushree {

static void findDuplicateFruits(String[] fruits) {


Map<String, Integer> fruitCountMap = new HashMap<>();

for (String fruit : fruits) {


fruitCountMap.put(fruit, fruitCountMap.getOrDefault(fruit,
0) + 1);
}

System.out.println("Duplicates:");
for (Map.Entry<String, Integer> entry :
fruitCountMap.entrySet()) {
if (entry.getValue() > 1) {
System.out.println(entry.getKey() + ":" +
entry.getValue());
}
}
}

public static void main(String[] args) {


String[] fruits = {"apple", "orange", "banana", "apple",
"mango", "orange", "kiwi"};

findDuplicateFruits(fruits);
}
}

Input : "apple", "orange", "banana", "apple", "mango", "orange", "kiwi"

Output : Duplicates: [apple:2, orange:2]


Question 5) Write a program to check if two given strings are anagrams of
each other.
import java.util.Arrays;

public class Anushree {

static boolean areAnagrams(String str1, String str2) {


str1 = str1.replaceAll("\\s", "").toLowerCase();
str2 = str2.replaceAll("\\s", "").toLowerCase();
if (str1.length() != str2.length()) {
return false;
}
char[] arr1 = str1.toCharArray();
char[] arr2 = str2.toCharArray();

Arrays.sort(arr1);
Arrays.sort(arr2);
return Arrays.equals(arr1, arr2);
}

public static void main(String[] args) {


String str1 = "eat";
String str2 = "tea";

if (areAnagrams(str1, str2)) {
System.out.println(str1 + " and " + str2 + " are
anagrams.");
} else {
System.out.println(str1 + " and " + str2 + " are not
anagrams.");
}

String str3 = "Silent";


String str4 = "listen";

if (areAnagrams(str3, str4)) {
System.out.println(str3 + " and " + str4 + " are
anagrams.");
} else {
System.out.println(str3 + " and " + str4 + " are not
anagrams.");
}
}
}

Input : str1 = "eat", str2 = "tea"

Output : eat and tea are anagrams.

Question 6) Write a Java program to check if a given string of parentheses is


balanced.
import java.util.Stack;
public class Anushree {

static boolean isBalanced(String str) {


Stack<Character> stack = new Stack<>();
for (char c : str.toCharArray()) {
if (c == '(' || c == '{' || c == '[') {
stack.push(c);
} else if (c == ')' || c == '}' || c == ']') {
if (stack.isEmpty()) {
return false;
}
char top = stack.pop();
if ((c == ')' && top != '(') ||
(c == '}' && top != '{') ||
(c == ']' && top != '[')) {
return false;
}
}
}

return stack.isEmpty();
}
public static void main(String[] args) {
String str1 = "([])";
if (isBalanced(str1)) {
System.out.println(str1 + " is balanced.");
} else {
System.out.println(str1 + " is not balanced.");
}

String str2 = "([)]";


if (isBalanced(str2)) {
System.out.println(str2 + " is balanced.");
} else {
System.out.println(str2 + " is not balanced.");
}
}
}

Input : str = "([])"

Output : "([])" is balanced.

Question 7) Rotate an array to the right by ‘k’ steps,where ‘k’ is a non-


negative number.
import java.util.Arrays;

public class Anushree {

static void rotateArray(int[] nums, int k) {


int n = nums.length;
k = k % n; // If k is larger than array length, reduce it
reverse(nums, 0, n - 1);
reverse(nums, 0, k - 1);
reverse(nums, k, n - 1);
}

static void reverse(int[] nums, int start, int end) {


while (start < end) {
int temp = nums[start];
nums[start] = nums[end];
nums[end] = temp;
start++;
end--;
}
}

public static void main(String[] args) {


int[] array = {1, 2, 3, 4, 5, 6};
int k = 4;

System.out.println("Original Array: " +


Arrays.toString(array));

rotateArray(array, k);

System.out.println("Rotated Array by " + k + " steps: " +


Arrays.toString(array));
}
}

Input : array = [1, 2, 3, 4, 5, 6] k=4

Output :

Original Array: [7, 8, 9, 10, 11]

Rotated Array by 2 steps: [10, 11, 7, 8, 9]

Question 8) Create a Java Singleton class


public class Singleton {
private static Singleton instance;
private Singleton() {
}
public static Singleton getInstance() {
if (instance == null) {
instance = new Singleton();
}
return instance;
}
public void exampleMethod() {
System.out.println("This is an example method of the Singleton
class.");
}

public static void main(String[] args) {


Singleton singleton = Singleton.getInstance();
singleton.exampleMethod();
}
}
Question 9) From a given array of natural numbers, return the distance
between the two closest numbers.
import java.util.Arrays;

public class ClosestNumbers {

static int distClosestNumbers(int[] nums) {


if (nums == null || nums.length < 2) {
return -1;
}
Arrays.sort(nums);
int minDistance = Integer.MAX_VALUE;
for (int i = 1; i < nums.length; i++) {
int distance = nums[i] - nums[i - 1];
if (distance < minDistance) {
minDistance = distance;
}
}

return minDistance;
}

public static void main(String[] args) {


int[] testArray = {3, 9, 50, 15, 99, 7, 98, 65};
int result = distClosestNumbers(testArray);
System.out.println("Minimum distance between closest numbers: "
+ result);
}
}

Input : int[] testArray = {3, 9, 50, 15, 99, 7, 98, 65};

Output : int[] testArray = {3, 9, 50, 15, 99, 7, 98, 65};


Question 10) How do you remove all occurrences of a given character from
the input string?
public class RemoveCharacter {

public static void main(String[] args) {


String str1 = "Australia";
char charToRemove = 'a';
str1 = str1.replace(String.valueOf(charToRemove), "");

System.out.println(str1);
}
}

Input :

String str1 = "Australia";

char charToRemove = 'a';

Output : ustrli

THE END

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy