ND23 QB3
ND23 QB3
Additional Instructions:
None
java.lang.ref
java.util.*
java.io.*
DataInputStream
FileInputStream
BufferedInputStream
PipeInputStream
Q3. A Java program can run on which of the following operating systems?
Unix
Windows
Linux
Q4. In Java byte, short, int and long all of these are
Signed
Unsigned
Q6. Which statements about calling the compilation command javac and the execution command java are true?
I only
II only
I and II
void
int
real
string
Q8. Which of these is used to perform all input & output operations in Java?
Streams
Variables
Classes
Methods
java.util package
java.lang package
java.io package
java.awt package
Q10. If a variable is marked as , every time the variable is used it must be read from the main memory. Similarly,
every time a variable is written, the value must be stored in the main memory.
shared
static
final
volatile
java.util
java.lang
system.lang
java.system
.java
.class
.exe
.byte
expressions: 14 <= 14
14 < 14
-9 > -25
-25 > -9
Tru
e
Tru
e
Tru
e
True
False
False
False
True
False
True
True
True
False
True
False
Q15. With x = 0, which of the following are legal lines of Java code for changing the value of x to 1?
1. x++;
2. x = x + 1;
3. x += 1;
4. x =+ 1;
1, 2 & 3
1&4
1, 2, 3 & 4
3&2
Q16. Increment operator ++ increases the value of the variable by what number?
Q17. Decrement operator −− decreases the value of the variable by what number?
12
11
15
x+=10;
x=+10;
x++10;
None of the listed options
Q21. What is the value of y after executing the following code snippet?
1 int x = 5;
2 int y = 2;
3 int z = x + (x>6 ? ++y : --y);
True
False
Compilation fails
An exception is thrown at runtime
Bus wins!
Car wins!
14
Numeric
Boolean
Characters
Integers
Floating–point numbers
Ball
Swim
Q28. What variable type of red allows the following application to compile?
1 package tornado;
2 public class Kansas {
3 public static void main(String[] args) {
4 int colorOfRainbow = 10;
5 red = 5;
6 switch(colorOfRainbow) {
7 default:
8 System.out.print("Home
");
9 break;
10 case red:
11 System.out.print("Away");
12 }
13 }
14 }
long
double
int
Q29. How many 1s are outputted when the following application is compiled and run?
1 package city;
2 public class Road {
3 public static void main(String... in) {
4 int intersections = 100;
5 int streets = 200;
6 if (intersections < 150) {
7 System.out.print("1");
8 }
9 else if (streets && intersections > 1000) {
10 System.out.print("2");
11 }
12 if (streets < 500)
13 System.out.print("1");
14 else
15 System.out.print("2");
16 }
17 }
One
Two
A case value must match the data type of the switch variable, or
be able to be promoted to that type.
Q32. Given the following code snippet, assuming dayOfWeek is an int, what variable type of Saturday is not permitted?
1 final saturday = 6;
2 switch(dayOfWeek) {
3 default:
4 System.out.print("Another Weekday");
5 break;
6 case saturday:
7 System.out.print("Weekend!"); }
byte
long
int
Jump!
Hop!
Exception
Q34. statement provides an easy way to dispatch execution to different parts of your code based on
the value of an expression.
if-else
switch
if
while
Q35. A switch statement can have case statements and default statements.
Error
15
25
No Output
Plan B
Plan C
if()
for()
continue
break
Q40. What will happen when you compile and run the following code?
0 0, 0 1, 0 2, 1 0, 1 1, 1 2, 2 0, 2 1, 2 2,
0 0, 0 1, 0 2, 0 3, 1 0, 1 1, 1 2, 1 3, 2 0, 2 1, 2 2, 2 3,
1 0, 2 0, 2 1,
Q41. How many of the loop types (while, do while, traditional for, and enhanced for) allow you to write code that
creates an infinite loop?
One
Two
Three
Four
Q42. Which of the following iterates a different number of times than the others?
No Output
10
10 (10 times)
Q44. What will happen when you compile and run the following code?
1 public class Test{
2 public static void main(String []args){
3 int i = 0;
4 for(i = 0; i < 10; i+
+){
5 continue;
6 }
7 System.out.println(
i);
8 }
9 }
10
11
Q45. How many of these statements can be inserted after the println to have the code flow follow the arrow in
this diagram? break;
break label1;
break label2;
Three
One
Two
Q46. What will happen when you compile and run the following code?
1
9
Q47. Which of the following can loop through an array without referring to the elements by index?
do-while loop
for (traditional)
for-each
while
Q48. What will happen when you compile and run the following code?
Q50. In while and do-while loops, a statement causes control to be transferred directly to the conditional
expression that controls the loop.
break
pause
start
continue
Q51. What will happen when you compile and run the following code?
garbage value
garbage value
Exception
Names
Values
Primitive
Object
10 20 30 40 50
10 20 30 40
10 20 30 40 50 60
00000
Q61. Which of the following declarations of the array contains the error?
int x[ ]= int[10];
float d[ ]= {1,2,3};
1 2 3 4 5 6 7 8 9 10
0 1 2 3 4 5 6 7 8 9 10
ijklmnopqr
iiiiiiiiii
Row
Column
Q64. What is the index variable for the element at the first row and first column in array a?
a[0][0]
a[1][1]
a[0][1]
a[1][0]
11
10
13
14
7.3
7.9
9.2
Q67. The in a two-dimensional array specifies the element's row and column position.
Superscript
Subscript
Row number
Column number
10
11
Q70. The subscripts are than the row and column in which the element is located
One number less
One-dimensional array
Two-dimensional array
Three-dimensional array
N-dimensional array
Q72. Assume int[][] x = {{1, 2}, {3, 4}, {5, 6}}, What are x.length are x[0].length?
1 and 2
2 and 3
3 and 2
3 and 3
7.3
7.9
9.2
One subscript
Two subscripts
Three subscripts
Zero subscript
Q75. How many elements are array matrix (int[][] matrix = new int[5][5])?
20
25
30
35
Ho World
Hlo World
Hello World
Hello Wd
Q77. Which of these methods of class String is used to extract a single character from a String object?
CHARAT()
charat()
charAt()
ChatAt()
false
true
-3
Q79. What is the string contained in s after the following lines of code?
1 StringBuffer s new StringBuffer(“Hello”);
2 s.deleteCharAt(0);
Hell
ello
Hel
llo
Q80. What is the value returned by function compareTo() if the invoking string is less than the string compared?
zero
java.util
java.lang
ArrayList
one
two
onetwo
twoone
21
138
1353
null NullPointerException
NullPointerException NullPointerException
Q86. Which of these operators can be used to concatenate two or more String objects?
+=
&
||
a default character
no characters in it
number of characters in it
Q88. toString() method is defined in
java.lang.String
java.lang.Object
java.lang.util
Q89. How many objects will be created for the following code?
BCD
ABC
ACD
CBA
123
abc
true
false
an int value
Q93. Which constructor creates an empty string buffer with the specified capacity as length?
StringBuffer()
StringBuffer(String str)
StringBuffer(int capacity)
false
true
0
Q95. Which of these methods of class String is used to obtain the length of a string object?
get()
Sizeof()
lengthof()
length()
true
false
-1
1 class Main{
2 public static void main(String args[])
3 {
4 String s1="java";
5 String s2="java";
6 System.out.println(s1.equals(s
2));
7 System.out.println(s1==s2);
8 }
9 }
true true
true false
false true
false false
true
false
Pattern class
matcher class
PatternSyntaxException
Regex class
Q102. What is the significance of the Matcher class for a regular expression in Java?
group *
group 0
group * or group 0
Q104. Java provides the package for pattern matching with regular expressions.
regex
java.regex
util.regex
java.util.regex
Q105. Which method returns the start index of the previous match?
Q106. Which of the following statements about the regex API are true?
Replace all other than the first subsequence of that matches pattern
with a replacement string
Q109. The method that performs the search-and-replace operation to strings for pattern matching is
searchandreplace()
add()
edit()
replace()
Matcher class
Pattern class
String class
Section 2 - Coding
Section Summary
● No. of Questions: 34
● Duration: 100 min
Additional Instructions:
None
Input Format
Output Format
Constraints
d d
Time Limit: - ms Memory Limit: - kb Code Size: - kb
Write a code by declaring three variables where two variables are of integer type and one variable is double.
Multiply the two integer variables and store the result in the remaining variable (double).
Question Instructions:
Input Format
The input consists of two integer values n1 and n2 separated by a single space.
Output Format
The output displays the result in double as shown in the sample output.
Constraints
67 801 53667.0
45 5 225.0
Write a Java program to get the integer values and print the same integer values.
Input Format
Output Format
26 26
If the marks of Alice in 3 subjects are mark1, mark2, mark3. Write a program to calculate the total and average.
Input Format
96 98 94 Total : 288
Average : 96.00
Write a program to find the square, cube, and square root of the given number.
Input Format
Output Format
The output displays the square, cube, and square root of the number.
Constraints
Question Instructions:
Input Format
The input consists of two integers separated by space representing N1 as the customers that he already has and N2 denotes
the customer added today.
Output Format
The output prints the total number of customers that Jeevan has as shown in the sample output.
Constraints
10 11 21
Sample Input Sample Output
4 5 9
Input Format
Output Format
places. Constraints
2 3 8.00
3 4 81.00
Salary Computation
Danny has recently got his job offer as an Event Concept Creator at Sparsh Event Services. The Company has sent
him a detailed salary structure with details of his basic salary, HRA and DA. The Company has promised to pay him
as under:
If his basic salary is less than Rs. 15000, then HRA = 15% of basic salary and DA = 90% of basic salary.
If his basic salary is either equal to or above Rs. 15000, then HRA = Rs. 5000 and DA = 98% of
basic salary. If Danny’s salary is given as input, write a program to find his gross salary.
Input Format
Output Format
The output displays the double value that refers to the gross salary of Danny. Display the output correct to 2 decimal places.
Constraints
12000 24600.00
Sample Input Sample Output
30000 64400.00
Grade Calculation
Write a program to display the grade of a student. If the mark is > 85 then display 'A', if the mark is > 75 then
display 'B', mark is > 65 then display 'C' for others display 'D'.
Input Format
Output Format
The output displays the respective grade for the student based on the mark provided as shown in the sample output. It
should print as "Invalid" if N<0.
Constraints
67 C
-50 Invalid
Given an integer as an input, it represents the temperature in centigrade. Determine the weather conditions
based on the following temperatures.
●Temperature < 0 then print "Freezing weather".
●Temperature 0 - 10 then print "Very cold weather".
●Temperature 10 - 20 then print "Cold weather".
●Temperature 20 - 30 then print "Normal in temperature".
●Temperature 30 - 40 then print "Its hot".
●Temperature >= 40 then print "Its very hot".
Input Format
Output Format
The output prints the weather conditions based on the conditions mentioned in the problem statement.
Constraints
-2 Freezing weather
Sample Input Sample Output
10 Cold weather
20 Normal in temperature
30 Its hot
Write a program to generate the first 'n' terms of the following series 1, 4, 9, 16, 25,...
Input Format
The input consists of an integer 'n' which denotes the number of terms to be printed in the series.
Output Format
5 1 4 9 16 25
4 1 4 9 16
Lucas Sequence
a = 0, b=0, c=1 are the 1st three terms. All other terms in the Lucas sequence are generated by the sum of their 3 most
recent predecessors. Write a program to generate the first n terms of a Lucas Sequence.
Input Format
The input contains an integer 'n' which denotes the given number.
Output Format
Print the 'n' terms of the Lucas Sequence, separated by a single space. There are no leading or trailing spaces in the output.
Constraints
5 0 0 1 1 2
4 0 0 1 1
SPECIAL NUMBER
Write a program to find all special numbers between the given range m and n (both inclusive). Assume that m and n
are 2-digit numbers.
A 2-digit number is said to be a special number if the sum of its digits and the products of its digits is equal to the
The digits in 19 are 1 and 9. The sum of the digits is 10 and the product of the
digits is 9. So, the answer is 10+9 = 19.
Input Format
The input consists of 2 integers m and n on separate lines denoting the range.
Output Format
Print the special numbers in separate lines as shown in the sample output.
Constraints
11 19
30 29
28 29
60 39
49
Trendy Numbers
Write a program to check whether the given number is a trendy number or not. A number is said to be a trendy
number if and only if it has 3 digits and the middle digit is divisible by 3.
Input Format
The input contains an integer 'n' which denotes the given number.
Output Format
If the given number is trendy, then print "Trendy Number". Otherwise, print "Not a Trendy Number".
Constraints
Input Format
Output Format
The output displays the smallest and largest number in the array in separate lines.
Constraints
5 smallest value: 2
12 4 2 5 22 largest value: 22
6 smallest value: 4
20 30 50 4 71 100 largest value: 100
Time Limit: - ms Memory Limit: - kb Code Size: - kb
Given an array A consists of N number of elements. If the sum of the elements is "even" print the sum of the
element. If the sum of the elements is "odd" print the product of the element.
Input Format
Output Format
Constraints
5 16
1 2 3 4 6
4 530400
10 20 52 51
Given an array of elements, write a program to find two elements in the array such that their sum is equal to the
given element K.
Input Format
Output Format
The output prints whether the array has a pair of elements with the given sum as shown in the sample output.
Constraints
Weighing machines in Sunrise Logistics are not working. Raju, the manager of the division wants to calculate the total
weight of received goods. Weight is printed on the goods label. Write a suitable code to help Raju.
Input Format
Output Format
Constraints
10 54
1 9 2 8 3 7 4 6 8 6
Input Format
Output Format
Constraints
3 3 15
1 2 3
4 5 6
4 4 191
12 23 45 56
78 89 98 87
Input Format
Output Format
The output prints the product of two matrices as shown in the sample output.
Constraints
3 33 28 35
1 2 3 75 70 83
4 5 6 47 42 51
Snake Pattern
Write a program to print the matrix in a zig-zag form.
Input Format
Input to get the integer R and C (i.e., rows and columns) in the first line separated by a single space, then get the values for
the R*C matrix in the following lines.
Output Format
Constraints
4 3 1 2 3 6 5 4 7 8 9 12 11 10
1 2 3
4 5 6
Write a program to obtain a matrix and find the sum of each row and each column.
Input Format
The first line of the input consists of the value of the number of rows and the number of columns
separated by space. The next lines of input is the matrix.
Output Format
The output prints the sum of each row and each column in separate lines as shown in the sample output.
Constraints
Input Format
The input consists of a string S.
Output Format
The output displays the number of vowels present in the given string.
Constraints
S>0
string 1
Input Format
Output Format
harry 0
harry
john -3
mini
tiger 8
lion
Input Format
Output Format
Input Format
Output Format
Input Format
Output Format
234 234
a12 0
You are given a ticket code. Please determine, whether CSK Team will win the match or not based on
Dhilip’sconviction. Print "YES" or "NO" (without quotes) corresponding to the situation.
Note:
Two letters x, y where x != y are said to be alternating in a code, if the code is of form "xyxyxy...".
Input Format
The first and only line of the input contains a string S denoting the letter code on the ticket.
Output Format
Output a single line containing "Yes" (without quotes) based on the conditions given and "No" otherwise.
ABABAB Yes
ABC No
XYXYX Yes
Adjacent characters
Given a string, write a program to compute a new string where identical chars that are adjacent in the original string
are separated from each other by a "*".
Input Format
Output Format
hello hel*lo
aaabbb a*a*ab*b*b
Let's implement the logic for mobile number validation using StringBuilder and embed it in our program. Mobile
number should precede with "+91", followed by 10 digits. The indexOf() method returns an index of a given
character value or substring. If it is not found, it returns -1. Write a program to validate the mobile number given as
input. Use indexOf() to check whether "+91" is present or not.
Create a driver class called Main. In the Main method, obtain the inputs from the console, validate the mobile
number and prompt the user as given in sample I/O.
Input Format
Output Format
Input Format
Output Format
The output prints the input text and results with whether the match is found or not found.
Constraints
abcdef abcdef
Not matched!
abcdef3459 abcdef3459
Found a
match!
Input Format
The input consists of different inputs of integer, string, double, and date with different formats.
Date formats :
1. dd/mm/yyyy : eg: 01/12/2022
2. mm/dd/yyyy : eg: 12/24/2022
3. dd-mon-yy : eg: 01-apr-22
4. dd-mon-yyyy : eg: 01-apr-2022
5. dd-month-yy: eg:01-april-22
6. dd-month-yyyy: eg: 01-april-2022
Output Format
Input Format
Output Format
The output prints the input and returns the match whether it is true or false.
ABCDEFabcdef123456 ABCDEFabcdef123456
true
W3.com W3.com
false
Rules:
The allowed characters are:
1. a to z, A to Z, 0 to 9, -,#
2. The 1st character should be an alphabet symbol only.
3. The length of the identifier should be at least 2.
Input Format
Output Format
Solution
No Solution
Q2
BufferedInputStream
Solution
No Solution
Q3
All of the listed options
Solution
No Solution
Q4
Signed
Solution
No Solution
Q5
It can be run on any computer with a compatible JVM.
Solution
No Solution
Q6
I and II
Solution
No Solution
Q7
void
Solution
No Solution
Q8
Streams
Solution
No Solution
Q9
java.lang package
Solution
No Solution
Q10
volatile
Solution
No Solution
Q11
java.lang
Solution
No Solution
Q12
It prevents Java bytecode from being easily decoded/decompiled.
Solution
No Solution
Q13
.class
Solution
No Solution
Q14
True
Fals
True
Fals
Solution
No Solution
Q15
1, 2, 3 & 4
Solution
Operator ++ increases the value of the variable by 1. x = x + 1 can also be written in shorthand form as x += 1. Also, x =+
No Solution
Q17
1
Solution
No Solution
Q18
12
Solution
No Solution
Q19
x+=10;
Solution
No Solution
Q20
Solution
No Solution
Q21
1
Solution
No Solution
Q22
Compilation fails
Solution
No Solution
Q23
Car wins!
Solution
No Solution
Q24
14
Solution
No Solution
Q25
Solution
No Solution
Q26
Solution
No Solution
Q27
Solution
No Solution
Q28
Solution
No Solution
Q29
Solution
No Solution
Q30
2
Solution
No Solution
Q31
Solution
No Solution
Q32
long
Solution
No Solution
Q33
The code doesn't compile.
Solution
No Solution
Q34
switch
Solution
No Solution
Q35
Solution
No Solution
Q36
15
Solution
No Solution
Q37
Solution
No Solution
Q38
2
Solution
No Solution
Q39
if()
Solution
No Solution
Q40
1 0, 2 0, 2 1,
Solution
No Solution
Q41
Three
Solution
No Solution
Q42
Solution
No Solution
Q43
Solution
No Solution
Q44
10
Solution
No Solution
Q45
Two
Solution
No Solution
Q46
0
Solution
No Solution
Q47
for-each
Solution
No Solution
Q48
Solution
No Solution
Q49
2
Solution
No Solution
Q50
continue
Solution
No Solution
Q51
Solution
No Solution
Q52
Solution
No Solution
Q53
0
Solution
No Solution
Q54
Solution
No Solution
Q55
Object
Solution
No Solution
Q56
10 20 30 40 50
Solution
No Solution
Q57
Class name@ hashcode in hexadecimal form
Solution
No Solution
Q58
Solution
No Solution
Q59
1
Solution
No Solution
Q60
1
Solution
No Solution
Q61
int x[ ]= int[10];
Solution
No Solution
Q62
iiiiiiiiii
Solution
No Solution
Q63
Row
Solution
No Solution
Q64
a[0][0]
Solution
No Solution
Q65
10
Solution
No Solution
Q66
7.9
Solution
No Solution
Q67
Subscript
Solution
No Solution
Q68
9
Solution
No Solution
Q69
Solution
No Solution
Q70
Solution
No Solution
Q71
Two-dimensional array
Solution
No Solution
Q72
3 and 2
Solution
No Solution
Q73
Solution
No Solution
Q74
Two subscripts
Solution
No Solution
Q75
25
Solution
No Solution
Q76
Hlo World
Solution
No Solution
Q77
charAt()
Solution
No Solution
Q78
-3
Solution
No Solution
Q79
ello
Solution
No Solution
Q80
Solution
No Solution
Q81
java.lang
Solution
No Solution
Q82
onetwo
Solution
No Solution
Q83
1353
Solution
No Solution
Q84
null NullPointerException
Solution
No Solution
Q85
Solution
No Solution
Q86
+
Solution
No Solution
Q87
no characters in it
Solution
No Solution
Q88
java.lang.Object
Solution
No Solution
Q89
3
Solution
No Solution
Q90
BCD
Solution
No Solution
Q91
abc
Solution
No Solution
Q92
an int value
Solution
No Solution
Q93
StringBuffer(int capacity)
Solution
No Solution
Q94
true
Solution
No Solution
Q95
length()
Solution
No Solution
Q96
-1
Solution
No Solution
Q97
true true
Solution
No Solution
Q98
false
Solution
No Solution
Q99
All characters k are replaced by a.
Solution
No Solution
Q10
0
Regex class
Solution
No Solution
Q10
1
Solution
No Solution
Q10
2
Solution
No Solution
Q10
3
group 0
Solution
No Solution
Q10
4
java.util.regex
Solution
No Solution
Q10
5
Solution
No Solution
Q10
6
The package java.util.regex includes an exception called PatternSyntaxException
Solution
No Solution
Q107
returns start index of the previous match
Solution
No Solution
Q10
8
Replace every subsequence of the input sequence that matches pattern with a replacement string
Solution
No Solution
Q10
9
replace()
Solution
No Solution
Q11
0
Solution
No Solution
Q11
1
Pattern class
Solution
No Solution
Section 2 - Coding
Q1
Test Case
Input Output
e e
Weightage - 25
Input Output
a a
Weightage - 10
Input Output
g g
Weightage - 25
Input Output
z z
Weightage - 40
d d
Solution
import java.util.*;
class Main
{
public static void main(String args[])
{
Scanner s=new
Scanner(System.in); char
a=s.next().charAt(0);
System.out.print(a);
}
}
Q2
Test Case
Input Output
30 111 3330.0
Weightage - 30
Input Output
-2 -3 6.0
Weightage - 5
Input Output
22 55 1210.0
Weightage - 5
Input Output
90 1800.0
20
Weightage - 10
Input Output
29 232.0
8
Weightage - 5
Input Output
Weightage - 30
Input Output
Weightage - 10
Input Output
-89 56 -4984.0
Weightage - 5
67 801 53667.0
45 5 225.0
Solution
import java.util.Scanner;
class Main{
public static void main(String args[])
{ int x,y;
double z;
Scanner sc=new Scanner(System.in);
x=sc.nextInt();
y=sc.nextInt();
z=x*y;
System.out.println(z);
}
}
Q3
Test Case
Input Output
15 15
Weightage - 100
26 26
Solution
import java.util.*;
class Main
{
public static void main(String args[])
{
Scanner s=new
Scanner(System.in); int
n=s.nextInt();
System.out.print(n);
}
}
Q4
Test Case
Input Output
84 85 81 Total : 250
Average : 83.00
Weightage - 10
Input Output
75 77 79 Total : 231
Average : 77.00
Weightage - 25
Input Output
62 63 65 Total : 190
Average : 63.00
Weightage - 25
Input Output
91 96 98 Total : 285
Average : 95.00
Weightage - 40
96 98 94 Total : 288
Average : 96.00
Solution
import java.util.Scanner;
import java.text.*;
class Main {
public static void main(String []
args) { int mark1, mark2, mark3;
Scanner sc = new Scanner(System.in);
mark1 = sc.nextInt();
mark2 = sc.nextInt();
mark3 = sc.nextInt();
DecimalFormat d = new DecimalFormat("0.00");
System.out.println("Total : "+(mark1+mark2+mark3)+"\nAverage : "+d.format(((mark1+mark2+mark3)/3)));
}
}
Q5
Test Case
Input Output
Weightage - 10
Input Output
Weightage - 10
Input Output
Weightage - 20
Input Output
Weightage - 20
Input Output
Weightage - 40
Solution
import java.util.*;
import java.util.Scanner;
class j5
{
public static void main(String
args[]){ Scanner sc=new
Scanner(System.in); int num;
num=sc.nextInt();
System.out.println("Square of "+ num + " is: "+ Math.pow(num, 2));
System.out.println("Cube of "+ num + " is: "+ Math.pow(num, 3));
System.out.println("Square Root of "+ num + " is: "+ Math.sqrt(num));
}
}
Q6
Test Case
Input Output
23 24 47
Weightage - 15
Input Output
2 3 5
Weightage - 5
Input Output
6 15
9
Weightage - 5
Input Output
4 12
8
Weightage - 5
Input Output
11 33
22
Weightage - 5
Input Output
20 50
30
Weightage - 15
Input Output
23 66
43
Weightage - 25
Input Output
40 90
50
Weightage - 25
10 11 21
4 5 9
Solution
import java.util.Scanner;
class Main
{
public static void main(String[] args)
{
int a,b,c;
Scanner sc=new Scanner(System.in);
a=sc.nextInt();
b=sc.nextInt();
c=a+b;
System.out.print(c);
}
}
Q7
Test Case
Input Output
1 1.00
2
Weightage - 10
Input Output
3 9.00
2
Weightage - 10
Input Output
2 32.00
5
Weightage - 20
Input Output
2 256.00
8
Weightage - 20
Input Output
3 729.00
6
Weightage - 40
3 4 81.00
Solution
import java.util.Scanner;
import java.lang.Math;
import java.text.*;
class Main {
public static void main(String []
args) { int base, power;
Scanner sc = new Scanner(System.in);
DecimalFormat d = new
DecimalFormat("0.00"); base =
sc.nextInt();
power = sc.nextInt();
System.out.println(d.format(Math.pow(base,power)));
}
}
Q8
Test Case
Input Output
8000 16400.00
Weightage - 5
Input Output
10000 20500.00
Weightage - 5
Input Output
7000 14350.00
Weightage - 5
Input Output
5000 10250.00
Weightage - 5
Input Output
15000 34700.00
Weightage - 15
Input Output
20000 44600.00
Weightage - 15
Input Output
45000 94100.00
Weightage - 25
Input Output
40000 84200.00
Weightage - 25
12000 24600.00
30000 64400.00
Solution
import java.io.*;
import java.util.*;
import java.text.*;
class Salarycomputation {
public static void main(String []
args) { int salary;
double amount,hra,da;
DecimalFormat d = new
DecimalFormat("0.00"); Scanner sc = new
Scanner(System.in);
salary = sc.nextInt();
if(salary < 15000) {
hra =
0.15*salary; da =
0.90*salary;
amount = salary+hra+da;
}
else {
da = 0.98*salary;
amount = salary+5000+da;
}
System.out.println(d.format(amount));
}
}
Q9
Test Case
Input Output
90 A
Weightage - 20
Input Output
79 B
Weightage - 20
Input Output
-44 Invalid
Weightage - 10
Input Output
71 C
Weightage - 10
Input Output
-1 Invalid
Weightage - 30
Input Output
80 B
Weightage - 10
67 C
-50 Invalid
Solution
import java.util.Scanner;
class Main{
public static void main(String args[])
{ int mark;
Scanner in =new Scanner(System.in);
mark=in.nextInt();
if(mark>=85){
System.out.print("A");
}
else if(mark>=75){
System.out.print("B");
}
else if(mark>=65){
System.out.print("C");
}
else if(mark>=0){
System.out.print("D");
}
else{
System.out.print("Invalid");
}
}
}
Q10
Test Case
Input Output
-4 Freezing weather
Weightage - 10
Input Output
Weightage - 10
Input Output
Weightage - 15
Input Output
16 Cold weather
Weightage - 25
Input Output
28 Normal in temperature
Weightage - 25
Input Output
33 Its hot
Weightage - 15
-2 Freezing weather
10 Cold weather
20 Normal in temperature
Sample Input Sample Output
30 Its hot
Solution
import java.io.*;
import java.util.*;
class Main {
public static void main(String []
args) { int temp;
Scanner sc = new
Scanner(System.in); temp =
sc.nextInt();
if(temp<0)
System.out.println("Freezing
weather"); else if(temp>=0 && temp<10)
System.out.println("Very cold
weather"); else if(temp>=10 && temp<20)
System.out.println("Cold
weather"); else if(temp>=20 &&
temp<30)
System.out.println("Normal in
temperature"); else if(temp>=30 && temp<40)
System.out.println("Its hot");
else if(temp>=40)
System.out.println("Its very hot");
}
}
Q11
Test Case
Input Output
10 1 4 9 16 25 36 49 64 81 100
Weightage - 20
Input Output
Weightage - 20
Input Output
Input Output
8 1 4 9 16 25 36 49 64
Weightage - 20
Input Output
7 1 4 9 16 25 36 49
Weightage - 20
5 1 4 9 16 25
4 1 4 9 16
Solution
import java.util.* ;
class Main
{
public static void main(String[] args)
{
Scanner sc = new
Scanner(System.in) ; int a =
sc.nextInt() ;
for(int i=1;i<=a;i++)
System.out.print(i*i+" ") ;
}
}
Q12
Test
Case
Output
Input
10 0 0 1 1 2 4 7 13 24 44
Weighta - 25
ge
Output
Input
12 0 0 1 1 2 4 7 13 24 44 81 149
Weighta - 30
ge
Output
Input
4 0 0 1 1
Weighta - 25
ge
Output
Input
8 0 0 1 1 2 4 7 13
Weighta - 10
ge
Output
Input
6 0 0 1 1 2 4
Weighta - 10
ge
Sample Input Sample Output
5 0 0 1 1 2
4 0 0 1 1
Solution
import
java.util.Scanner; class
Main
{
public static void main(String[] args)
{
int num;
Scanner s= new Scanner(System.in);
num=s.nextInt();
Main l= new Main();
l.lucas(num);
}
private void lucas(int num)
{
int thirdLast =
0; int secondLast
= 0; int last =
1;
int current = 0;
System.out.print(thirdLast + " " + secondLast + " " + last + "
"); for (int i = 3; i < num; i++)
{
current = last + secondLast + thirdLast;
System.out.print(current + " ");
int tmp =
last; last =
current;
thirdLast = secondLast;
secondLast = tmp;
}
}
}
Q13
Test Case
Input Output
20 29
60 39
49
Weightage - 15
Input Output
30 39
50 49
Weightage - 15
Input Output
60 69
120 79
89
Weightage - 30
Input Output
100 109
180 119
129
Weightage - 40
11 19
30 29
28 29
60 39
49
Solution
import java.util.Scanner;
class Main
{
public static void main(String args[])
{
int n,m,sum=0,i,pro=1,sum1,j;
Scanner io=new Scanner(System.in);
m=io.nextInt();
n=io.nextInt();
for(i=m;i<=n;i++)
{
j=i;
int
first=i/10;
int last=i%10;
sum=first+last
;
pro=first*last
;
sum1=sum+pro;
if(sum1==j)
System.out.println(sum1);
}
}
}
Q14
Test
Case
Output
Input
Weighta - 25
ge
Output
Input
Weightage - 10
Inp
ut
Out
put
768 768 is trendy number
Weightage - 40
Input Output
Weightage - 25
Solution
import java.util.Scanner;
class Main
{
public static void main(String args[])
{
int a,b,count=0;
Scanner s = new Scanner(System.in);
a=s.nextInt();
int c=a;
System.out.print(a+" is ");
while (a != 0)
{
a /= 10;
++count;
}
if(count!=3)
{
System.out.println("not a trendy number");
}
else
{
b = (c / 10) % 10
; if(b%3==0)
{
System.out.println("trendy number");
}
els
e
{
System.out.println("not a trendy number");;
}
}
}
}
Q15
Test Case
Input Output
10 smallest value: 9
23 34 13 64 72 90 10 15 9 27 largest value: 90
Weightage - 30
Input Output
6 smallest value: 4
20 30 50 4 71 100 largest value: 100
Weightage - 15
Input Output
16 smallest value: 1
20 30 50 4 71 100 23 34 13 64 72 90 largest value: 100
Weightage - 20
Input Output
4 smallest value: 7
7 8 9 40 largest value: 40
Weightage - 15
Input Output
9 smallest value: 12
98 87 65 74 25 254 45 12 41 largest value: 254
Weightage - 20
6 smallest value: 4
20 30 50 4 71 100 largest value: 100
Solution
import java.util.Scanner;
class Large_Small{
public static void main (String args[])
{
Scanner scan=new
Scanner(System.in); int min,max;
int n=scan.nextInt();//get input from user for array
length int arr[]=new int[n]; //declaring an array of n
elements
//for loop takes input from
user for(int i=0; i<n; i++){
arr[i]=scan.nextInt();//takes input from user for array
}
min=arr[0];//assume first element as smallest
value max=arr[0];//assume first element as
largest value for(int i=0; i<n; i++){
if(min>arr[i]){//loop for find minimum
elements min=arr[i];
}
if(max<arr[i]){
max=arr[i]; //loop for find maximum elements
}
}
System.out.print("smallest value: "+min);
System.out.print("\nlargest value: "+max);
}
}
Q16
Test Case
Input Output
3 100
44 52 4
Weightage - 10
Input Output
7 1095791004
77 99 11 33 22 3 6
Weightage - 10
Input Output
10 482
12 34 56 78 90 20 34 52 40 66
Weightage - 25
Input Output
12 266338304
44 32 101 122 432 566 766 844 942 10 12 98
Weightage - 25
Input Output
30 5150
12 34 55 7 8 9 0 3 2 9 56 43 78 23 91 39
321
Weightage - 30
5 16
1 2 3 4 6
4 530400
10 20 52 51
Solution
import java.util.Scanner;
class Main {
public static void main(String args[])
{ int n;
Scanner in = new
Scanner(System.in); n =
in.nextInt();
int i;
int[] array = new
int[n]; for (i = 0; i <
n; i++)
array[i] = in.nextInt();
int sum = 0, mul = 1;
for (int num : array) {
sum = sum + num;
mul = mul * num;
}
i = (su
sum % 2 == 0) ? sum : mul;
System.out.println(i);
}
}
Q17
Test Case
Input Output
Weightage - 20
Input Output
Weightage - 25
Input Output
Weightage - 25
Input Output
Weightage - 30
Solution
import java.util.*;
class Test {
static boolean
hasArrayTwoCandidates( int
A[],
int arr_size, int sum)
{
int l, r;
Arrays.sort(A)
; l = 0;
r = arr_size - 1;
while (l < r) {
if (A[l] + A[r] == sum)
return true;
else if (A[l] + A[r] < sum)
l++;
else
r--;
r--;
}
return false;
}
public static void main(String args[])
{
int i,n,sum;
Scanner sc = new
Scanner(System.in); n =
sc.nextInt();
int [] arr = new
int[n]; for(i=0;i<n;i+
+) {
arr[i] = sc.nextInt();
}
sum = sc.nextInt();
if (hasArrayTwoCandidates(arr, n, sum))
System.out.println("Array has two "
+ "elements with given sum "+sum);
else
System.out.println("Array doesn't have "
+ "two elements with given sum "+sum);
}
}
Q18
Test Case
Input Output
10 30973
1222 1339 476 1215 6024 7188 1962 558 1970
9019
Weightage - 10
Input Output
25 140365
8323 2552 121 4006 1439 841 4714 5932 9347 7975
9
Weightag 10
e -
Input Output
60 28744
997 290 242 766 59 467 418 522 793 424 985 784 4
Weightage - 15
Input Output
41 201124
5713 8117 8687 3137 9417 3123 4279 2944 5757
5186
Weightage - 15
Input Output
200 972712
1407 2551 9381 7624 9985 7805 6038 2585 1390
4909
Weightage - 25
Input Output
200 755388
-1407 2551 -9381 7624 9985 7805 6038 2585
1390 49
Weightage - 25
10 54
1 9 2 8 3 7 4 6 8 6
Solution
import java.util.Scanner;
class SumDemo{
public static void main(String args[])
{ int count;
int sum;
sum=0;
Scanner scan = new Scanner(System.in);
count = scan.nextInt();
int array[] = new int[count];
for (int i = 0; i < count; i++)
{
array[i] = scan.nextInt();
}
scan.close();
for( int num : array) {
sum = sum+num;
}
System.out.println(sum);
}
}
Q19
Test Case
Input Output
3 3 15
1 2 3
4 5 6
Weightage - 10
Input Output
4 4 191
12 23 45 56
78 89 98
87
Weightage -
25
Input Output
2 2 126
74 85
96 52
Weightage - 25
Input Output
1 1 25
25
Weightage - 40
3 3 15
1 2 3
4 5 6
4 4 191
12 23 56
45
78 89 87
98
Solution
import java.util.Scanner;
class disgonalSum
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int
i,j,row,col,sum=0;
row = sc.nextInt();
col = sc.nextInt();
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
if(i==j)
{
sum = sum + mat[i][j];
}
}
}
System.out.println(sum) ;
}
}
Q20
Test Case
Input Output
Weightage - 10
Input Output
Weightage - 25
Input Output
Weightage - 25
Input Output
Weightage - 40
3 33 28 35
1 2 3 75 70 83
4 5 6 47 42 51
Solution
import java.io.*;
import java.util.*;
class Main {
public static void main(String []
args) { int n,i,j,k;
Scanner sc = new
Scanner(System.in); n =
sc.nextInt();
int mat1[][] = new int[n][n];
int mat2[][] = new int[n][n];
int m[][] = new int[n][n];
for(i=0;i<n;i++) {
for(j=0;j<n;j++) {
mat1[i][j] = sc.nextInt();
}
}
for(i=0;i<n;i++) {
for(j=0;j<n;j++) {
mat2[i][j] = sc.nextInt();
}
}
for(i=0;i<n;i++) {
for(j=0;j<n;j++) {
m[i][j] =0;
for(k=0;k<n;k++) {
m[i][j] += mat1[i][k]*mat2[k][j];
}
}
}
for(i=0;i<n;i++) {
for(k=0;k<n;k++) {
System.out.print(m[i][k]+" ");
}
System.out.println();
}
}
}
Q21
Test Case
Input Output
5 3 3 4 5 9 8 7 23 45 67 0 99 88 12 15 16
3 4 5
7 8 9
Weightage - 30
Input Output
2 2 34 37 78 45
34 37
45 78
Weightage - 15
Input Output
3 2 5 6 87 89 45 67
5 6
89 87
Weightage - 15
Input Output
4 4 12 34 56 78 90 54 76 98 22 33 44 55 99 88
12 34 56 78 77 66
98 76 54 90
Weightage - 20
Input Output
Weightage - 20
4 3 1 2 3 6 5 4 7 8 9 12 11 10
1 2 3
4 5 6
Solution
import java.util.Scanner;
class Main
{
static void printZigZag(int row, int col, int a[][])
{
int evenRow = 0;
int oddRow = 1;
int r, c;
int i,j;
Scanner in=new Scanner(System.in);
r=in.nextInt();
c=in.nextInt();
int mat[][]=new int[10][10];
for(i=0;i<r;i++){
for(j=0;j<c;j++){
mat[i][j]=in.nextInt();
}
}
printZigZag(r , c , mat);
}
}
Q22
Test Case
Input Output
Weightage - 25
Input Output
Weightage - 25
Input Output
Weightage - 10
Input Output
Weightage - 40
Solution
import
java.util.Scanner; class
matrix {
static void row_sum(int arr[][],int m, int n)
{
int i, j, sum = 0;
for (i = 0; i < m; ++i) {
for (j = 0; j < n; ++j) {
sum = sum + arr[i][j];
}
System.out.println("Sum of the
he row
ow " + i + " = "
+ sum);
sum = 0;
}
}
static void column_sum(int arr[][], int m,int n)
{
int i, j, sum = 0;
for (i = 0; i < m; ++i) {
for (j = 0; j < n; ++j)
{ sum = sum + arr[j]
[i];
}
System.out.println("Sum of the column " + i
+ " = " + sum);
sum = 0;
}
}
public static void main(String[] args)
{
int i, j,m,n;
Scanner sc = new Scanner(System.in);
m = sc.nextInt();
n = sc.nextInt();
int[][] arr = new int[m][n];
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
arr[i][j] = sc.nextInt();
row_sum(arr,m,n);
column_sum(arr,m,n);
}
}
Q23
Test Case
Input Output
lemon 2
Weightage - 10
Input Output
follow 2
Weightage - 10
Input Output
member 2
Weightage - 20
Input Output
tree 2
Weightage - 20
Input Output
yellow 2
Weightage - 40
string 1
Solution
import java.util.Scanner;
Q24
Test Case
Input Output
nisha -2
priya
Weightage - 10
Input Output
richard -6
roy
Weightage - 10
Input Output
solo 6
minion
Weightage - 20
Input Output
alloy -12
metal
Weightage - 20
Input Output
unique 1
talent
Weightage - 40
harry 0
harry
john -3
mini
tiger 8
lion
Solution
import java.util.Scanner;
class Main{
public static void main(String
args[]) { Scanner in=new
Scanner(System.in);
String str1,str2;
str1=in.nextLine();
str2=in.nextLine();
int result = str1.compareTo( str2 );
System.out.println(result);
}
}
Q25
Test Case
Input Output
Weightage - 10
Input Output
Weightage - 10
Input Output
Weightage - 20
Input Output
computer computer
Weightage - 20
Input Output
Weightage - 40
networking networking
Solution
import java.util.Scanner;
Q26
Test Case
Input Output
Weightage - 20
Input Output
Weightage - 20
Input Output
Weightage - 30
Input Output
Weightage - 10
Input Output
Weightage - 10
Input Output
Weightage - 10
Solution
import java.util.*;
import java.util.Scanner;
class Main
{
public static void main(String args[])
{
String str, reverse = "";
Scanner in = new
Scanner(System.in); str =
in.nextLine();
Q27
Test Case
Input Output
786 786
Weightage - 10
Input Output
456 456
Weightage - 25
Input Output
4851 4851
Weightage - 25
Input Output
a1234b 0
Weightage - 40
234 234
a12 0
Solution
import
java.util.*;
import
java.lang.*;
import java.io.*;
class Q01Medium_StringAdv {
Q28
Test Case
Input Output
ABABAB Yes
Weightage - 10
Input Output
ABC No
Weightage - 10
Input Output
XYXYX Yes
Weightage - 10
Input Output
IJIJIJ Yes
Weightage - 15
Input Output
KLKLK Yes
Weightage - 15
Input Output
IJK No
Weightage - 10
Input Output
IJIJK No
Weightage - 20
Input Output
WXYZ No
Weightage - 10
ABABAB Yes
ABC No
XYXYX Yes
Solution
import java.io.*;
import java.util.*;
class AlternatingCode {
public static void main(String []
args) { int i,s=0;
String a;
char x,y;
Scanner sc = new
Scanner(System.in); a =
sc.next();
x =
a.charAt(0); y
= a.charAt(1);
if(x==y) {
s=1;
}
else{
for(i=0;i<a.length();i++)
{
if(i%2==0) {
if(x!=a.charAt(i)) {
s=1;
break;
}
}
else if(y!= a.charAt(i)) {
s=1;
break;
}
}
}
if(s==1) {
System.out.println("No");
}
else {
System.out.println("Yes");
}
}
}
Q29
Test Case
Input Output
hello hel*lo
Weightage - 10
Input Output
aabb a*ab*b
Weightage - 10
Input Output
abba ab*ba
Weightage - 10
Input Output
abbaab ab*ba*ab
Weightage - 15
Input Output
aaabbb a*a*ab*b*b
Weightage - 15
Input Output
abbabaab ab*baba*ab
Weightage - 20
Input Output
aaaabbbb a*a*a*ab*b*b*b
Weightage - 20
hello hel*lo
aaabbb a*a*ab*b*b
Solution
import java.io.*;
import java.util.*;
class AdjacentCharacters {
public static void main(String [] args)
{ String s;
Scanner sc = new
Scanner(System.in); s =
sc.next();
pairStar(s,0);
System.out.println(s1);
for(int i=0; i<s.length();i++)
{ s1 = s1+s.charAt(i);
if(i == s.length()-1) {
return;
}
if(s.charAt(i) == s.charAt(i+1))
{ s1 = s1+'*';
}
}
}
static String s1="";
private static void pairStar(String s, int
i) { s1 = s1+s.charAt(i);
if(i == s.length()-1) {
return;
}
if(s.charAt(i) == s.charAt(i+1))
{ s1 = s1+'*';
}
pairStar(s,i+1);
}
}
Q30
Test Case
Input Output
Weightage - 15
Input Output
Weightage - 15
Input Output
Weightage - 20
Input Output
Weightage - 20
Input Output
Weightage - 30
Solution
import java.io.*;
import java.util.*;
class Main {
public static void main(String []
args) { Scanner sc = new
Scanner(System.in); String mobile
= sc.nextLine();
if(mobile.substring(0, 3).compareTo("+91") == 0 && mobile.substring(3).length() == 10) {
System.out.println("Mobile number valid");
}
else {
System.out.println("Mobile number invalid");
}
}}
Q31
Test Case
Input Output
3abcdef 3abcdef
Not matched!
Weightage - 10
Input Output
abcdef9 abcdef9
Found a match!
Weightage - 25
Input Output
ghijklm ghijklm
Not matched!
Weightage - 25
Input Output
defg21494 defg21494
Found a
match!
Weightage - 40
abcdef abcdef
Not matched!
abcdef3459 abcdef3459
Found a
match!
Solution
import java.util.*;
import
java.util.regex.Matcher;
import
java.util.regex.Pattern;
class Main
{
if (m.find())
return "Found a match!";
else
return "Not matched!";
}
}
Q32
Test Case
Input Output
Weightage - 10
Input Output
Weightage - 25
Input Output
Weightage - 30
Input Output
21-apr-1994 The datatype of 21-apr-1994 is: java.util.Date
Weightage - 25
Input Output
Weightage - 10
Solution
import java.util.*;
class Main
{
public static void main(String[] arg)
{
String input;
Scanner s = new Scanner(System.in);
input = s.nextLine();
String dataType = null;
Q33
Test Case
Input Output
SQL SQL
true
Weightage - 10
Input Output
Java Java
true
Weightage - 25
Input Output
www3.com www3.com
false
Weightage - 25
Input Output
!@#$ !@#$
false
Weightage - 40
ABCDEFabcdef123456 ABCDEFabcdef123456
true
W3.com W3.com
false
Solution
import java.util.Scanner;
class Main
{
public static void main(String[] args)
{
Scanner s = new
Scanner(System.in); String str;
str=s.nextLine();
System.out.println(str);
System.out.println(validate(str));
}
public static boolean validate(String text)
{
return text.matches("^[\\w]+$");
}
}
Q34
Test Case
Input Output
_number _number:Invalid Identifier
Weightage - 10
Input Output
Weightage - 25
Input Output
Weightage - 25
Input Output
Weightage - 40
Solution
import java.util.regex.*;
import java.util.*;
class Main
{
public static void main(String[] args)
{
Pattern p=Pattern.compile("[a-zA-Z][a-zA-Z0-9-#]
+"); Scanner s = new Scanner(System.in);
String str;
str=s.nextLine();
Matcher m=p.matcher(str);
if(m.find()&&m.group().equals(str))
{
System.out.print(str+":"+"Valid Identifier");
}
else
{
System.out.print(str+":"+"Invalid Identifier");
}
}
}