0% found this document useful (0 votes)
2 views19 pages

Tcs NQT Basic Coding Questions Java Master Sheet

The document contains a collection of basic coding questions and solutions in Java, covering a range of topics such as string manipulation, array operations, and number theory. Each coding problem is presented with its corresponding Java code implementation, including tasks like reversing strings, finding prime numbers, and sorting arrays. Additionally, it includes important pattern questions and frequently asked logic coding questions relevant for TCS interviews.

Uploaded by

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

Tcs NQT Basic Coding Questions Java Master Sheet

The document contains a collection of basic coding questions and solutions in Java, covering a range of topics such as string manipulation, array operations, and number theory. Each coding problem is presented with its corresponding Java code implementation, including tasks like reversing strings, finding prime numbers, and sorting arrays. Additionally, it includes important pattern questions and frequently asked logic coding questions relevant for TCS interviews.

Uploaded by

Akshaya G
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

TCS NQT Basic Coding Questions — Java

Master Sheet

1. Reverse a String

import [Link].*;

public class Main {


public static void main(String[] args) {
String str = "hello";
String rev = "";

for(int i = [Link]() - 1; i >= 0; i--) {


rev += [Link](i);
}

[Link](rev);
}
}
2. Palindrome String

import [Link].*;

public class Main {


public static void main(String[] args) {
String str = "madam";
String rev = "";

for(int i = [Link]() - 1; i >= 0; i--) {


rev += [Link](i);
}

if([Link](rev))
[Link]("Palindrome");
else
[Link]("Not Palindrome");
}
}

3. Reverse an Array

import [Link].*;

public class Main {


public static void main(String[] args) {
int[] arr = {1,2,3,4,5};

for(int i = [Link] - 1; i >= 0; i--) {


[Link](arr[i] + " ");
}
}
}
4. Find Largest Element in Array

import [Link].*;

public class Main {


public static void main(String[] args) {
int[] arr = {10, 50, 30, 90, 20};
int max = arr[0];

for(int i = 1; i < [Link]; i++) {


if(arr[i] > max)
max = arr[i];
}

[Link](max);
}
}

5. Find Smallest Element in Array

import [Link].*;

public class Main {


public static void main(String[] args) {
int[] arr = {10, 50, 30, 90, 20};
int min = arr[0];

for(int i = 1; i < [Link]; i++) {


if(arr[i] < min)
min = arr[i];
}

[Link](min);
}
}
6. Sum of Array Elements

import [Link].*;

public class Main {


public static void main(String[] args) {
int[] arr = {1,2,3,4,5};
int sum = 0;

for(int num : arr)


sum += num;

[Link](sum);
}
}

7. Find Second Largest Element

import [Link].*;

public class Main {


public static void main(String[] args) {
int[] arr = {10, 20, 4, 45, 99};
int first = Integer.MIN_VALUE;
int second = Integer.MIN_VALUE;

for(int num : arr) {


if(num > first) {
second = first;
first = num;
} else if(num > second && num != first) {
second = num;
}
}

[Link](second);
}
}
8. Count Vowels in String

import [Link].*;

public class Main {


public static void main(String[] args) {
String str = "education";
int count = 0;

str = [Link]();

for(int i = 0; i < [Link](); i++) {


char ch = [Link](i);

if(ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u')


count++;
}

[Link](count);
}
}
9. Check Prime Number

import [Link].*;

public class Main {


public static void main(String[] args) {
int n = 29;
boolean prime = true;

if(n <= 1)
prime = false;

for(int i = 2; i <= [Link](n); i++) {


if(n % i == 0) {
prime = false;
break;
}
}

if(prime)
[Link]("Prime");
else
[Link]("Not Prime");
}
}
10. Print Prime Numbers in Range

import [Link].*;

public class Main {


public static void main(String[] args) {
int start = 1, end = 50;

for(int n = start; n <= end; n++) {


boolean prime = true;

if(n <= 1)
prime = false;

for(int i = 2; i <= [Link](n); i++) {


if(n % i == 0) {
prime = false;
break;
}
}

if(prime)
[Link](n + " ");
}
}
}
11. Fibonacci Series

import [Link].*;

public class Main {


public static void main(String[] args) {
int n = 10;
int a = 0, b = 1;

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


[Link](a + " ");
int c = a + b;
a = b;
b = c;
}
}
}

12. Factorial of Number

import [Link].*;

public class Main {


public static void main(String[] args) {
int n = 5;
int fact = 1;

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


fact *= i;
}

[Link](fact);
}
}
13. Armstrong Number

import [Link].*;

public class Main {


public static void main(String[] args) {
int n = 153;
int temp = n;
int sum = 0;

while(n > 0) {
int digit = n % 10;
sum += digit * digit * digit;
n /= 10;
}

if(sum == temp)
[Link]("Armstrong");
else
[Link]("Not Armstrong");
}
}

14. Swap Two Numbers

public class Main {


public static void main(String[] args) {
int a = 10, b = 20;

int temp = a;
a = b;
b = temp;

[Link](a + " " + b);


}
}
15. Sort Array

import [Link].*;

public class Main {


public static void main(String[] args) {
int[] arr = {5,2,8,1,9};

[Link](arr);

for(int num : arr)


[Link](num + " ");
}
}

16. Linear Search

public class Main {


public static void main(String[] args) {
int[] arr = {1,2,3,4,5};
int key = 4;
boolean found = false;

for(int num : arr) {


if(num == key) {
found = true;
break;
}
}

if(found)
[Link]("Found");
else
[Link]("Not Found");
}
}
17. Binary Search

import [Link].*;

public class Main {


public static void main(String[] args) {
int[] arr = {1,2,3,4,5,6,7};
int key = 5;

int low = 0, high = [Link] - 1;

while(low <= high) {


int mid = (low + high) / 2;

if(arr[mid] == key) {
[Link]("Found");
return;
}
else if(arr[mid] < key)
low = mid + 1;
else
high = mid - 1;
}

[Link]("Not Found");
}
}
18. Remove Duplicates from Array

import [Link].*;

public class Main {


public static void main(String[] args) {
int[] arr = {1,2,2,3,4,4,5};

HashSet<Integer> set = new HashSet<>();

for(int num : arr)


[Link](num);

[Link](set);
}
}

19. Count Frequency of Characters

import [Link].*;

public class Main {


public static void main(String[] args) {
String str = "programming";

HashMap<Character, Integer> map = new HashMap<>();

for(char ch : [Link]()) {
[Link](ch, [Link](ch, 0) + 1);
}

[Link](map);
}
}
20. Check Anagram

import [Link].*;

public class Main {


public static void main(String[] args) {
String s1 = "listen";
String s2 = "silent";

char[] a = [Link]();
char[] b = [Link]();

[Link](a);
[Link](b);

if([Link](a, b))
[Link]("Anagram");
else
[Link]("Not Anagram");
}
}
Important Pattern Questions

21. Star Pattern Triangle

public class Main {


public static void main(String[] args) {
int n = 5;

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


for(int j = 1; j <= i; j++) {
[Link]("* ");
}
[Link]();
}
}
}

22. Number Pattern

public class Main {


public static void main(String[] args) {
int n = 5;

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


for(int j = 1; j <= i; j++) {
[Link](j + " ");
}
[Link]();
}
}
}
Frequently Asked TCS Logic Coding
Questions

23. Sum of Digits

public class Main {


public static void main(String[] args) {
int n = 1234;
int sum = 0;

while(n > 0) {
sum += n % 10;
n /= 10;
}

[Link](sum);
}
}

24. Reverse Number

public class Main {


public static void main(String[] args) {
int n = 1234;
int rev = 0;

while(n > 0) {
rev = rev * 10 + n % 10;
n /= 10;
}

[Link](rev);
}
}
25. Palindrome Number

public class Main {


public static void main(String[] args) {
int n = 121;
int temp = n;
int rev = 0;

while(n > 0) {
rev = rev * 10 + n % 10;
n /= 10;
}

if(temp == rev)
[Link]("Palindrome");
else
[Link]("Not Palindrome");
}
}
Most Important Interview-Level Array
Questions

26. Move Zeros to End

import [Link].*;

public class Main {


public static void main(String[] args) {
int[] arr = {0,1,0,3,12};
int index = 0;

for(int i = 0; i < [Link]; i++) {


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

while(index < [Link]) {


arr[index++] = 0;
}

[Link]([Link](arr));
}
}
27. Find Missing Number

public class Main {


public static void main(String[] args) {
int[] arr = {1,2,4,5};
int n = 5;

int total = n * (n + 1) / 2;
int sum = 0;

for(int num : arr)


sum += num;

[Link](total - sum);
}
}

28. Check if Array is Sorted

public class Main {


public static void main(String[] args) {
int[] arr = {1,2,3,4,5};
boolean sorted = true;

for(int i = 0; i < [Link] - 1; i++) {


if(arr[i] > arr[i + 1]) {
sorted = false;
break;
}
}

if(sorted)
[Link]("Sorted");
else
[Link]("Not Sorted");
}
}
Very Important String Questions

29. Remove Spaces from String

public class Main {


public static void main(String[] args) {
String str = "Hello World Java";

str = [Link]("\\s", "");

[Link](str);
}
}

30. Capitalize First Letter

public class Main {


public static void main(String[] args) {
String str = "hello";

str = [Link](0,1).toUpperCase() + [Link](1);

[Link](str);
}
}

You might also like