Sie sind auf Seite 1von 46

4/29/2019 CU Exams

CSE_CS104_Practice12_2019
Test Summary
No. of Sections: 2
No. of Questions: 55
Total Duration: 11000 min

Section 1 - MCQs

Section Summary
No. of Questions: 42
Duration: 1000 min

Additional Instructions:
None

Q1. Which of the following statements is correct to create a DataOutputStream to write to a file named out.dat?

DataOutputStream outfile = new DataOutputStream("out.dat");

DataOutputStream outfile = new DataOutputStream(new


FileOutputStream("out.dat"));

DataOutputStream outfile = new DataOutputStream(FileOutputStream("out.dat")); 

None of the above

1 Predict the output of given program


Q2.
2
3 import java.io.*;
4 class files
5 {
6 public static void main(String args[])
7 {
8 File obj = new File("/java/system");
9 System.out.print(obj.getName());
10 }
11 }
12

java

system

java/system

/java/system

Q3. Which method is used to establish the connection with the specified url in a Driver Manager class? 

public static void registerDriver(Driver driver)

https://admin.exam.chitkara.edu.in/test/preview?test_id=5b1ffa62-7e65-4864-8951-6872d9906a74&branch_id=7a4955c1-ae99-4872-9896-2126f0bb… 1/46
4/29/2019 CU Exams
public static void deregisterDriver(Driver driver)

public static Connection getConnection(String url,String userName,String


password)

public static Connection getConnection(String url)

Q4. Which of these values is returned by read() method is end of file (EOF) is encountered?

-1

NULL

Q5. ..........is the name of abstract class for streams that dealing with general purpose(non character) input.

Input Stream

Output Stream

Reader

Writer

Q6. Which of the following method is not included in InputStream class.

available()

reset()

flush()

close( )

Q7. For character input which streams base class is used.

Reader

Writer

Input Stream

https://admin.exam.chitkara.edu.in/test/preview?test_id=5b1ffa62-7e65-4864-8951-6872d9906a74&branch_id=7a4955c1-ae99-4872-9896-2126f0bb… 2/46
4/29/2019 CU Exams
Output Stream

Q8. Which of these exception is thrown in cases when the file specified for writing is not found?

IOException

FileException

FileNotFoundException

FileInputException

Q9. Which of the following is used to call stored procedure?

Statement

PreparedStatement

CallableStatment

CalledStatement

Q10.  Which of these class is used to read characters in a file?

FileReader

FileWriter

FileInputStream

InputStreamReader

Q11. Which of following option can be used for the input stream class which uses the character array as a source?

BufferedInputStream

CharArrayReader                         

FileArrayReader

BufferedReader

Q12. Which method is used to establish the connection with the specified url in a Driver Manager class? 

https://admin.exam.chitkara.edu.in/test/preview?test_id=5b1ffa62-7e65-4864-8951-6872d9906a74&branch_id=7a4955c1-ae99-4872-9896-2126f0bb… 3/46
4/29/2019 CU Exams
public static void registerDriver(Driver driver)

public static void deregisterDriver(Driver driver)

public static Connection getConnection(String url)

None of the above

Q13. Which of these class contains the methods used to write in a file?

FileStream

FileOutputStream

BufferedOutputStream

FileBufferStream

Q14. Which of the following is correct about JDBC?

The JDBC API provides the abstraction and the JDBC drivers provide the
implementation.

New drivers can be plugged-in to the JDBC API without changing the client code.

Both of the above.

None of the above.

Q15. Which streams provide functionally for threads to communicate and exchange data between them.

Object

Piped

Pushback

Filtered

Q16. ……………………. class is used to increase the efficiency of input operations.

DataInputStream

FileInputStream

https://admin.exam.chitkara.edu.in/test/preview?test_id=5b1ffa62-7e65-4864-8951-6872d9906a74&branch_id=7a4955c1-ae99-4872-9896-2126f0bb… 4/46
4/29/2019 CU Exams
BufferedInputStream

PipeInputStream

Q17. Which one of the following contains date information?

java.sql.TimeStamp

java.sql.Time

java.io.Time

java.io.TimeStamp

Q18. Which IO package class is used to store a collection of object in a file?

ObjectOutputStream

StringReader

 Writer

PrintStream

Q19. Which of the following is not a JDBC connection isolation levels?

TRANSACTION_READ_COMMITTED

TRANSACTION_REPEATABLE_READ

TRANSACTION_NONE

TRANSACTION_NONREPEATABLE_READ

Q20. Which statement is static and synchronized in JDBC API?

executeQuery()

executeUpdate()

getConnection()

prepareCall()

Q21. Which exception is thrown by read() method?


https://admin.exam.chitkara.edu.in/test/preview?test_id=5b1ffa62-7e65-4864-8951-6872d9906a74&branch_id=7a4955c1-ae99-4872-9896-2126f0bb… 5/46
4/29/2019 CU Exams

IOException

InterruptedException

SystemException

SystemInputException

Q22. Which of these class is used to read characters and strings in Java from console?

BufferedReader

 StringReader

BufferedStreamReader

InputStreamReader

Q23. If there are comparing java.io.BufferedWriter and java.io.FileWriter class, then which capability exist as a method in only one of two ?

writing a line separator to the stream                     

opening to the stream

flushing the stream

closing the stream

Q24. Which of the following package can define the System class

java.awt package

java.util package

java.lang package                      

java.io package

1 Predict the output of following code.


Q25.
2
3 import java.io.*;
4
5 public class mcqs
6
7 {
8 public static void main(String[] args)
9 {
10 try {
11 StringReader st=new StringReader("Hi 1 Examly");
12 StreamTokenizer strT=new StreamTokenizer(st);
https://admin.exam.chitkara.edu.in/test/preview?test_id=5b1ffa62-7e65-4864-8951-6872d9906a74&branch_id=7a4955c1-ae99-4872-9896-2126f0bb… 6/46
4/29/2019 13 while(strT.nextToken()!=StreamTokenizer.TT_EOF)CU Exams
14 {
15 System.out.println(strT.sval +" "+strT.nval);
16 }
17 }
18 catch(Exception e) {System.out.println(e);}
19 }
20 }

Error

Hi 0.0
null 1.0
Examly 1.0

Hi 0.0
null 1.0
Examly 0.0

Hi 0.0
Hi 1.0
Examly1.0

Q26. What happens if you call the method close() on a ResultSet object? 

the method close() does not exist for a ResultSet. Only Connections can be closed.

the ResultSet, together with the Statement which created it and the Connection
from which the Statement was retrieved, will be closed and release all database
and JDBC resources

the database and JDBC resources are released

you will get a SQLException, because only Statement objects can close ResultSets

Q27. Which of the following is advantage of using PreparedStatement in Java?

Prevents SQL injection

Encourages SQL injection

Slow performance

More memory usage

Q28. Which package contains classes that help in connecting to a database, sending SQL statements to the database, and processing the query
results.

connection.sql

db.sql

https://admin.exam.chitkara.edu.in/test/preview?test_id=5b1ffa62-7e65-4864-8951-6872d9906a74&branch_id=7a4955c1-ae99-4872-9896-2126f0bb… 7/46
4/29/2019 CU Exams
pkg.sql

java.sql

Q29. Which is correct about DriverManager class of JDBC.

JDBC DriverManager is a class that manages a list of database drivers.

It matches connection requests from the java application with the proper database
driver using communication subprotocol.

Both of the above.

none of the above

Q30. Predict the output of the following code?


import java.io.*;
   public class filesinputoutput
   {
          public static void main(String[] args)
       {
             String obj = "abc";
          byte b[] = obj.getBytes();
          ByteArrayInputStream obj1 = new ByteArrayInputStream(b);
          for (int i = 0; i < 2; ++ i)
          {
              int c;
              while((c = obj1.read()) != -1)
              {
                         if(i == 0)
                  {
                             System.out.print(Character.toUpperCase((char)c));
                      obj2.write(1);
                         }
              }
              System.out.print(obj2);
          }
       }
   }

AaBaCa

ABCaaa

AaaBaaCaa

AaBaaCaaa

Q31. The OutputStreams includes methods that are designed to perform the following tasks.
i) closing streams                       ii) flushing streams
iii) reading bytes                        iv) writing bytes

https://admin.exam.chitkara.edu.in/test/preview?test_id=5b1ffa62-7e65-4864-8951-6872d9906a74&branch_id=7a4955c1-ae99-4872-9896-2126f0bb… 8/46
4/29/2019 CU Exams
ii, iii and iv only

i, ii and iii only

i, ii and iv only

All i, ii, iii and iv

Q32. What is the output of this program?


1 import java.io.*;
2 class Chararrayinput{
3 public static void main(String[] args){
4 String obj = "abcdefgh";
5 int length = obj.length();
6 char c[] = new char[length];
7 obj.getChars(0, length, c, 0);
8 CharArrayReader input1 = new CharArrayReader(c);
9 CharArrayReader input2 = new CharArrayReader(c, 1, 4);
10 int i;
11 int j;
12 try{
13 while((i = input1.read()) == (j = input2.read())){
14 System.out.print((char)i);
15 }
16 }
17 catch (IOException e){
18 e.printStackTrace();
19 }
20 }
21 }

abc

abcd

abcde

none of the mentioned

Q33. What is the output of this program?


1 import java.io.*;
2 class filesinputoutput{
3 public static void main(String args[]){
4 InputStream obj = new FileInputStream("inputoutput.java");
5 System.out.print(obj.available());
6 }
7 }
8
9 Note: inputoutput.java is stored in the disk.

true

false

prints number of bytes in file

prints number of characters in the file


https://admin.exam.chitkara.edu.in/test/preview?test_id=5b1ffa62-7e65-4864-8951-6872d9906a74&branch_id=7a4955c1-ae99-4872-9896-2126f0bb… 9/46
4/29/2019 CU Exams
Q34. Which of the following type of JDBC driver, is also called Type 3 JDBC driver?

A - JDBC-ODBC Bridge plus ODBC driver

Native-API, partly Java driver

JDBC-Net, pure Java driver

Native-protocol, pure Java driver

Q35. Predict the output of following program?


   import java.io.*;
  class CharArrayClass {
    public static void main(String[] args) {
        String strr = "jklmno";
        int len = strr.length();
        char c[] = new char[len];
        strr.getChars(0,len,c,0);
     CharArrayReader obj1 = new CharArrayReader(c);
     CharArrayReader obj2 = new CharArrayReader(c, 0, 3);
           int i;
           try {
                 while ((i = obj1.read()) != -1) {
                   System.out.print((char)i);    }
                  }
           catch (IOException e) {
            System.out.print(e);    }
    }
   }

jklmn

jklm

jklmno                                     

jkl

Q36. Which system is of java program uses JDBC, that determine information directly from database system at runtime

Efficient System

Robust System

Active System

Accurate System

Q37. Which of the following is not a JDBC connection isolation levels?

TRANSACTION_NONE

https://admin.exam.chitkara.edu.in/test/preview?test_id=5b1ffa62-7e65-4864-8951-6872d9906a74&branch_id=7a4955c1-ae99-4872-9896-2126f0b… 10/46
4/29/2019 CU Exams
TRANSACTION_READ_COMMITTED

TRANSACTION_REPEATABLE_READ

TRANSACTION_NONREPEATABLE_READ

Q38. What is used to execute parameterized query?

Statement interface

PreparedStatement interface

ResultSet interface

None of the above

Q39. Which of the following is correct about DriverManager class of JDBC?

A - JDBC DriverManager is a class that manages a list of database drivers.

It matches connection requests from the java application with the proper database
driver using communication subprotocol.

Both of the above.

none of the above.

Q40. What does the Class.forName("MyClass") do?

A - Loads the class MyClass.

Execute any static block code of MyClass.

Returns an instance of MyClass.

All of the above.

Q41. Which type of Statement can be used to execute parameterized queries in JDBC?

Parameterized Statement

Prepared Statement

Callable Statement and Prepared Statement 

https://admin.exam.chitkara.edu.in/test/preview?test_id=5b1ffa62-7e65-4864-8951-6872d9906a74&branch_id=7a4955c1-ae99-4872-9896-2126f0b… 11/46
4/29/2019 CU Exams
All kinds of Statements

Q42. What can be happened if we call the deleteRow() method on a ResultSet object in JDBC?

The row you are positioned on is deleted only from the database, not from the
ResultSet .

The row you are positioned on is deleted only from the ResultSet, not from the
database.

The row we are positioned on is deleted from the ResultSet and the database  

We will get a compile time error:because we can not delete rows from a ResultSet 

Section 2 - CODING

Section Summary
No. of Questions: 13
Duration: 10000 min

Additional Instructions:
None

Q1. VOTERS LIST

The collector of Rajpura had ordered a complete revision of the Voters List. He knew that constructing the list of voters is a difficult task,
prone to errors. Some voters may have been away on vacation, others may have moved during the enrollment and so on.
To be as accurate as possible, he entrusted the task to three different officials. Each of them was to independently record the list of voters
and send it to the collector. In Rajpura, every one has an ID number and the list would only list the ID numbers of the voters and not their
names. The officials were expected to arrange the ID numbers in ascending order in their lists.
23 30 42 57 90
21 23 35 57 90 92
21 23 30 57 90
then the final list compiled by the collector would be:
21 23 30 57 90
The ID numbers 35, 42 and 92 which appeared in only one list each do not figure in the final list.
Your task is to help the collector by writing a program that produces the final list from the three given lists.

Input Format

The first line of the input contains 3 integers N1, N2 and N3. N1 is the number of voters in the first list, N2 is the number of voters in the second list
and N3 is the number of voters in the third list. The next N1 lines (lines 2,...,N1+1) contain one positive integer each and describe the first list in
ascending order. The following N2lines (lines N1+2,...,N1+N2+1) describe the second list in ascending order and the final N3 lines
(lines N1+N2+2,...,N1+N2+N3+1) describe the third list in ascending order.

Output Format

The first line of the output should contain a single integer M indicating the number voters in the final list. The next M lines (lines 2,...,M+1) should
contain one positive integer each, describing the list of voters in the final list, in ascending order.

Sample Input Sample Output

5 6 5 5
23 30 42 57 90 21
21 23 35 57 90 92 23
21 23 30 57 90 30
Sample Input Sample Output

https://admin.exam.chitkara.edu.in/test/preview?test_id=5b1ffa62-7e65-4864-8951-6872d9906a74&branch_id=7a4955c1-ae99-4872-9896-2126f0b… 12/46
4/29/2019 CU Exams
2 3 4 1
22 23 45
24 34 45
56 45 67 68
Time Limit: - ms Memory Limit: - kb Code Size: - kb

Q2. RECIPE INGREDIENTS

Chef has just found a recipe book, where every dish consists of exactly four ingredients. He is going to choose some two dishes and
prepare them for dinner. Of course, he likes diversity and wants to know whether the two dishes are similar. Two dishes are called similar if
at least half of their ingredients are the same. In other words, at least two of four ingredients of the first dish should also be present in the
second dish. The order of ingredients doesn't matter. Your task is to examine T pairs of dishes. For each pair, check if the two dishes are
similar and print "similar" or "dissimilar" accordingly.

Input Format

The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The first line of each test
case contains four distinct strings, denoting ingredients needed for the first dish. Each ingredient is represented by a string of length between 2 and 10
inclusive, consisting of lowercase English letters. The second line of each test case describes the second dish in the same format.

Output Format

For each test case, output a single line containing the answer — "similar" if at least half of the ingredients are same, and "dissimilar" otherwise (without
the quotes).

Sample Input Sample Output

2 [similar, dissimilar]
one two three four
one two three four
gibberish jibberish lalalalala popopopopo
Time Limit: - ms Memory Limit: - kb Code Size: - kb

Q3. Sliding Window Maximum

Rohan is a computer science student and his teacher give him a question:
Given an array nums, and with this there is a sliding window of size k which is moving from the very left of the array to the very right. You
can only see the k numbers in the window. Each time the sliding window moves right by one position. This window return the max number.

Input Format

First line contain the size of array


Next n lines contain elements of array
Last line store the size of k

Output Format

Maximum number from each window.

Sample Input Sample Output

8 7
5 7 -1 -1 -2 -3 2 5 7
2 -1
1
Time Limit: - ms Memory Limit: - kb Code Size: - kb

Q4. CANDIES
Badri is standing at the door of his classroom. There are currently N students in the class, i'th student got Ai candies.
There are still M more students to come inside the class. At every instant, a student enters the class and seated with a student who has
exactly the same number of candies. For each student, Badri shouts YES if such a student with same number of candies is found, NO
otherwise.

Input Format

First line contains an integer T. T test cases follow. First line of each case contains two space-separated integers N and M.
Second line contains N + M space-separated integers, the candies of the students.

Output Format

For each test case, output M new line, Badri's answer to the M students.
Print "YES" (without the quotes) or "NO" (without the quotes) pertaining to the Badri's answer.
https://admin.exam.chitkara.edu.in/test/preview?test_id=5b1ffa62-7e65-4864-8951-6872d9906a74&branch_id=7a4955c1-ae99-4872-9896-2126f0b… 13/46
Explanation (refer sample input & output below for more understanding):
4/29/2019 CU Exams
Initially students with 3 and 2 candies are in the class.
A student with 9 candies enters the class, no student with 9 candies in class. Hence, Badri shouts "NO"
A student with 11 candies enters the class, no student with 11 candies in class. Hence, Badri shouts "NO"
A student with 2 candies enters the class, student with 2 candies found in class. Hence, Badri shouts "YES"

Sample Input Sample Output

1 NO
2 3 NO
3 2 9 11 2 YES

Time Limit: - ms Memory Limit: - kb Code Size: - kb

Q5. Repeated String


Write a program to remove instances of words that are repeated more than once, but retain the first occurrence of any case-insensitive
repeated word.
For example in the sentence "Rohan is a good good boy", the word "good" is repeated. The program should turn "Rohan is a good good boy"
into "Rohan is a good boy".

Input Format

The first line contains an integer denoting the number of sentences.


Each of the subsequent lines contains a sentence.

Output Format

In each line should display the input sentences after removing the repeated words leaving only the first occurrence.

Sample Input Sample Output

5 Good bye world


Good bye bye world world world Sam went to his business
Sam went went to his his his business Reya is the best player
Reya is is the the best best player Hello
Time Limit: - ms Memory Limit: - kb Code Size: - kb

Q6. Dynamic size arrays using ArrayList.


You are given n lines. In each line there are zero or more integers. You need to answer a few queries where you need to tell the number
located in yth position of xth line. 

Input Format

The first line has an integer. 


In each of the next lines there will be an integer d denoting number of integers on that line and then there will be d space-separated integers. 
In the next line there will be an integer denoting number of queries. 
Each query will consist of two integers x and y.

Output Format

In each line, output the number located in yth position of xth line. 
If there is no such position, just print "ERROR!"

Sample Input Sample Output

5 74
5 41 77 74 22 44 52
1 12 37
4 37 34 36 52 ERROR!
Time Limit: - ms Memory Limit: - kb Code Size: - kb

Q7. Smallest & Largest substring of given length


Arrange the string in lexicographical order which means A<B<…..<a<b (as per ascii range)
for example : apple<bat
A substring of a string is a contiguous block of characters in the string. For example, the substrings of abc are a, b, c, ab, bc, and abc.
Given string s and integer k, complete the function so that it finds the lexicographical smallest and largest substring of length k.

Input Format

First line contains string s


Second line contains integer k.

Output Format
https://admin.exam.chitkara.edu.in/test/preview?test_id=5b1ffa62-7e65-4864-8951-6872d9906a74&branch_id=7a4955c1-ae99-4872-9896-2126f0b… 14/46
String should be 1<=s<=1000
4/29/2019 CU Exams
It should contain only alphabets like A-Z-a-z

Sample Input Sample Output

ABCXYZ ABC
3 XYZ

Time Limit: - ms Memory Limit: - kb Code Size: - kb

Q8. Calorie Count


Mostly People love chocolates, but they also have to stay fit. Each chocolate has a calorie count, and People can walk a distance to expend
or burn those calories. If People have eaten k chocolates so far, after eating a chocolates with c calories they must walk minimum (2k)x c
miles to maintain their weight
For example, if they eat chocolates with calorie counts in any order like [5 10 7], the miles they need to walk are ((20)x 5 + (21)x 10 + (22)x
7) = 5+20+28 =53. This is not the minimum, though, so we need to test other orders of consumption. In this case, our minimum miles is
calculated as ((20)x 10 + (21)x 7 + (22) x 5) = 10+14+20=44.

Input Format

The first line contains an integer n, the number of chocolates in calorie.


The second line contains n space-separated integers calorie[i].

Output Format

Print a long integer denoting the minimum number of miles People must walk to maintain their weight.

Sample Input Sample Output

5 57
5 3 4 2 1

Time Limit: - ms Memory Limit: - kb Code Size: - kb

Q9. TREASURE HUNT

You are given a grid with N rows (numbered 1 through N) and M columns (numbered 1 through M). Let's denote the cell in row r and column
c by (r, c). Two cells of the grid are adjacent if they share a side.
Some of the cells of this grid contain treasures. You do not know exactly which cells contain them, but an analysis of the grid, called a
treasure hunt map, is available. For each cell (i, j), you are given an integer A_{i, j} with the following meaning:
·        A_{i, j} = -1: no information
·        A_{i, j} = 0: there is an even number of cells containing a treasure which are adjacent to the cell (i, j)
·        A_{i, j} = 1: there is an odd number of cells containing a treasure which are adjacent to the cell (i, j)
A treasure layout is the set of all cells containing treasures. Find the number of possible treasure layouts that are consistent with all the
given information. Since the answer may be large, compute it modulo 10^9 + 7.

Input Format

1. ·        The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows.
2. ·        The first line of each test case contains two space-separated integers N and M.
3. ·        N lines follow. For each i (1 \le i \le N), the i-th of these lines contains M space-separated integers A_{i, 1}, A_{i, 2}, \ldots, A_{i, M}.

Output Format

For each test case, print a single line containing one integer — the number of treasure layouts modulo 10^9+7.

Sample Input Sample Output

1 8
3 4
2 3 3 2
2 1 1 1
Sample Input Sample Output

1 0
2 2
-1 1
0 1
Time Limit: - ms Memory Limit: - kb Code Size: - kb

Q10.
https://admin.exam.chitkara.edu.in/test/preview?test_id=5b1ffa62-7e65-4864-8951-6872d9906a74&branch_id=7a4955c1-ae99-4872-9896-2126f0b… 15/46
4/29/2019 CU Exams
After every six months, the department of an organization to maintain the employees list that contains employees name and salary
increment for their performance and give them feedback so that they can improve. To analyse Department required sorted list either on
base of name or salary increment.
To find the employees’s performance by sorting and display the employees's list. If “SalaryIncrementSort” is enter then list should be sorted
with salary increment in the descending order and if “NameSort” is entered as choice, then list should be sorted by name of employees in
alphabetic order. Write a program to complete analysis process.

Input Format
The first line of input will contain numbers of employees, n.
The next lines contain name and of salary increment.
The last line of input contains a choice whether to sort as SalaryIncrementSort or NameSort.
Output Format
Output contains serial no, name and Salary Increment according to sorting choice entered.

Sample Input 1
5
Samar
2500
Tarun
2300
Sara
2600
Gopal
2700
Kanika
3500
SalaryIncrementSort

Sample Output 1
1 Kanika 3500
2 Gopal 2700
3 Sara 2600
4 Samar 2500
5 Tarun 2300

Sample Input 2
6
Trishna
3000
Shanti
2300
Thara
2600
Shanti
3500
Kritka
2000
Latika
3600
NameSort
Sample Output 2
1 Kritka 2000
2 Latika 3600
3 Shanti 2300
4 Shanti 3500
5 Thara 2600
6 Trishna 3000

Sample Input Sample Output

5 1 Kanika 3500
Samar 2 Gopal 2700
2500 3 Sara 2600
Tarun 4 Samar 2500
Time Limit: - ms Memory Limit: - kb Code Size: - kb

Q11. Comparators are used to compare two objects. In this question, you'll create a comparator and use it to sort an array.
The Player class is provided for you in your editor. It has fields: a String and a integer.
Given an array of Player objects, write a comparator that sorts them in order of decreasing score; if or more players have the same score,
sort those players alphabetically by name. To do this, you must create a Checker class that implements the Comparator interface, then
write an int compare(Player a, Player b) method implementing the Comparator.compare(T o1, T o2) method. 

Input Format
https://admin.exam.chitkara.edu.in/test/preview?test_id=5b1ffa62-7e65-4864-8951-6872d9906a74&branch_id=7a4955c1-ae99-4872-9896-2126f0b… 16/46
Input is handled by the locked stub code in the Solution class. The first line contains
4/29/2019 an integer, n, denoting the number of players. Each of the n
CU Exams
subsequent lines contains a player’s name and score, respectively. Constraints players can have the same name. Player names consist of lowercase
English letters. 

Output Format

You are not responsible for printing any output. The locked stub code in Solution will create a Checker object, use it to sort the Player array, and print
each sorted element. 

Constraints

Players can have the same name.


Player names consist of lowercase English letters.

Sample Input Sample Output

5 aleksa 150
amy 100 amy 100
david 100 david 100
heraldo 50 aakansha 75
Time Limit: - ms Memory Limit: - kb Code Size: - kb

Q12. A string containing only parentheses is balanced if the following is true: 1. if it is an empty string 2. if A and B are correct, AB is correct, 3. if
A is correct, (A) and {A} and [A] are also correct.
Examples of some correctly balanced strings are: "{}()", "[{()}]", "({()})" 
Examples of some unbalanced strings are: "{}(", "({)}", "[[", "}{" etc.
Given a string, determine if it is balanced or not.

Input Format

There will be a line in the input file, each having a single non-empty string. 

Output Format

For this case, print 'true' if the string is balanced, 'false' otherwise.

Sample Input Sample Output

{}() true

Time Limit: - ms Memory Limit: - kb Code Size: - kb

Q13. 1. Write a program to find the First Two largest numbers and Last Two largest numbers of a Given array

Input Format

5 // Array Size
1
2
3
4
5

Output Format

First Largest two numbers 5 4


Last largest two numbers 2 1

Sample Input Sample Output

10 Enter Array SizeEnter the Elements


9 First two Largest two numbers are:9 and 8
8 Last two Largetst two Numbers are:1and0
7
Time Limit: - ms Memory Limit: - kb Code Size: - kb

https://admin.exam.chitkara.edu.in/test/preview?test_id=5b1ffa62-7e65-4864-8951-6872d9906a74&branch_id=7a4955c1-ae99-4872-9896-2126f0b… 17/46
4/29/2019 CU Exams
Answer Key & Solution
Section 1 - MCQs

Q1 DataOutputStream outfile = new DataOutputStream(new FileOutputStream("out.dat"));

Solution

No Solution

Q2 system

Solution

No Solution

Q3 public static Connection getConnection(String url)

Solution

No Solution

Q4 -1

Solution

No Solution

Q5 Input Stream

Solution

No Solution

Q6 flush()

Solution

No Solution

Q7 Reader

Solution

No Solution

Q8 FileNotFoundException

Solution

No Solution

https://admin.exam.chitkara.edu.in/test/preview?test_id=5b1ffa62-7e65-4864-8951-6872d9906a74&branch_id=7a4955c1-ae99-4872-9896-2126f0b… 18/46
4/29/2019 CU Exams
Q9 CallableStatment

Solution

No Solution

Q10 FileReader

Solution

No Solution

Q11 CharArrayReader                         

Solution

No Solution

Q12 public static Connection getConnection(String url)

Solution

No Solution

Q13 FileOutputStream

Solution

No Solution

Q14 Both of the above.

Solution

No Solution

Q15 Piped

Solution

No Solution

Q16 BufferedInputStream

Solution

No Solution

Q17 java.sql.TimeStamp

https://admin.exam.chitkara.edu.in/test/preview?test_id=5b1ffa62-7e65-4864-8951-6872d9906a74&branch_id=7a4955c1-ae99-4872-9896-2126f0b… 19/46
4/29/2019 CU Exams
Solution

No Solution

Q18 ObjectOutputStream

Solution

No Solution

Q19 TRANSACTION_NONREPEATABLE_READ

Solution

No Solution

Q20 getConnection()

Solution

No Solution

Q21 IOException

Solution

No Solution

Q22 BufferedReader

Solution

No Solution

Q23 writing a line separator to the stream                     

Solution

No Solution

Q24 java.lang package                      

Solution

No Solution

Q25 Hi 0.0

null 1.0

https://admin.exam.chitkara.edu.in/test/preview?test_id=5b1ffa62-7e65-4864-8951-6872d9906a74&branch_id=7a4955c1-ae99-4872-9896-2126f0b… 20/46
4/29/2019 CU Exams
Examly 1.0

Solution

No Solution

Q26 the database and JDBC resources are released

Solution

No Solution

Q27 Prevents SQL injection

Solution

No Solution

Q28 java.sql

Solution

No Solution

Q29 Both of the above.

Solution

No Solution

Q30 AaBaaCaaa

Solution

No Solution

Q31 i, ii and iv only

Solution

No Solution

Q32 none of the mentioned

Solution

No Solution

Q33 prints number of bytes in file

https://admin.exam.chitkara.edu.in/test/preview?test_id=5b1ffa62-7e65-4864-8951-6872d9906a74&branch_id=7a4955c1-ae99-4872-9896-2126f0b… 21/46
4/29/2019 CU Exams
Solution

No Solution

Q34 JDBC-Net, pure Java driver

Solution

No Solution

Q35 jklmno                                     

Solution

No Solution

Q36 Robust System

Solution

No Solution

Q37 TRANSACTION_NONREPEATABLE_READ

Solution

No Solution

Q38 PreparedStatement interface

Solution

No Solution

Q39 Both of the above.

Solution

No Solution

Q40 All of the above.

Solution

No Solution

Q41 Prepared Statement

Solution

https://admin.exam.chitkara.edu.in/test/preview?test_id=5b1ffa62-7e65-4864-8951-6872d9906a74&branch_id=7a4955c1-ae99-4872-9896-2126f0b… 22/46
4/29/2019 CU Exams
No Solution

Q42 The row we are positioned on is deleted from the ResultSet and the database  

Solution

No Solution

Section 2 - CODING

Q1 Test Case

Input Output

1 1 1 1
23 23
23
42

Weightage - 20

Input Output

1 2 3 2
23 23
23 45 45
45 56 67
Weightage - 20

Input Output

1 1 3 2
22 22
22 34
22 34 34
Weightage - 20

Input Output

2 2 3 2
23 34 1
1 3 34
1 34 56

Weightage - 20

Input Output

2 2 2 3
1 1 1
2 2 2
3 3 3

Weightage - 20

Sample Input Sample Output

5 6 5 5
https://admin.exam.chitkara.edu.in/test/preview?test_id=5b1ffa62-7e65-4864-8951-6872d9906a74&branch_id=7a4955c1-ae99-4872-9896-2126f0b…
23 30 42 57 90 21 23/46
4/29/2019 21 23 35 57 90 92 CU23Exams
21 23 30 57 90 30
Sample Input Sample Output

2 3 4 1
22 23 45
24 34 45
56 45 67 68
Solution

Header

import java.util.*;

public class file {

public static void main(String[] args) throws Exception{

Scanner sc = new Scanner(System.in);


int N1, N2, N3;
N1 = sc.nextInt();
N2 = sc.nextInt();
N3 = sc.nextInt();
ArrayList<Integer> voters = new ArrayList<>();
ArrayList<Integer> validVoters = new ArrayList<>();

int total = N1 + N2 + N3;


//System.out.println("total" + total);

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


voters.add(sc.nextInt());
}

Collections.sort(voters);

int count = 0;
int i = 0;
while(i < voters.size()){
count = 0;
int cid = voters.get(i);
int k = i;
while(k < voters.size() && cid == voters.get(k)){
k++;
count++;
}
if(count > 1){
validVoters.add(voters.get(i));
}
i = k;
}

Footer

System.out.println(validVoters.size());
for(int v: validVoters){
System.out.println(v);
}

https://admin.exam.chitkara.edu.in/test/preview?test_id=5b1ffa62-7e65-4864-8951-6872d9906a74&branch_id=7a4955c1-ae99-4872-9896-2126f0b… 24/46
4/29/2019 } CU Exams
}

Q2 Test Case

Input Output

5 [similar, similar, dissimilar, similar, dissimilar]


eggs sugar flour salt
sugar eggs milk flour
aa ab ac ad
Weightage - 20

Input Output

3 [similar, similar, dissimilar]


eggs sugar flour salt
sugar eggs milk flour
aa ab ac ad

Weightage - 20

Input Output

1 [similar]
milk cream onion potato
potato cheese cream water

Weightage - 20

Input Output

2 [dissimilar, dissimilar]
one two three four
five seven six four
gibberish jibberish lalalalala popopopopo

Weightage - 20

Input Output

1 [dissimilar]
sss ssd sse ssf
jjj kkk lll hhh

Weightage - 20

Sample Input Sample Output

2 [similar, dissimilar]
one two three four
one two three four
gibberish jibberish lalalalala popopopopo
Solution

Header

https://admin.exam.chitkara.edu.in/test/preview?test_id=5b1ffa62-7e65-4864-8951-6872d9906a74&branch_id=7a4955c1-ae99-4872-9896-2126f0b… 25/46
4/29/2019 CU Exams
import java.util.*;
public class file {

public static void main(String[] args) {

Scanner sc=new Scanner(System.in);


List<String> finale=new ArrayList<String>();

int t=0;
//System.out.println("number of test case");
t=sc.nextInt();
sc.nextLine();

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

int same=0;
String ingredients1[]=new String[4];
String ingredients2[]=new String[4];
//System.out.println("testCase "+i+" line1");
String line1=sc.nextLine();
//System.out.println("testCase "+i+" line2");
String line2=sc.nextLine();

ingredients1=line1.split(" ");
ingredients2=line2.split(" ");

for(int j=0;j<4;j++) {
for(int k=0;k<4;k++) {
if (ingredients1[j].equals(ingredients2[k]))same++;
}
}
if (same>=2)finale.add("similar");
if (same<2) finale.add("dissimilar");
}
System.out.println(finale);

Footer

Q3 Test Case

Input Output

9 6
6 4 -4 2 -1 -4 -6 -2 10 4
4 2
2
Weightage - 30

Input Output

https://admin.exam.chitkara.edu.in/test/preview?test_id=5b1ffa62-7e65-4864-8951-6872d9906a74&branch_id=7a4955c1-ae99-4872-9896-2126f0b… 26/46
4/29/2019 CU Exams
10 8
5 8 2 -2 -7 -7 -4 9 10 11 8
5 2
9

Weightage - 30

Input Output

5 18
11 18 -18 13 -13 18
2 13
13

Weightage - 20

Input Output

7 29
29 20 -20 11 10 -2 -5 20
3 11
11

Weightage - 20

Sample Input Sample Output

8 7
5 7 -1 -1 -2 -3 2 5 7
2 -1
1
Solution

Header

import java.util.*;
public class file
{
public static int[] maxSlidingWindow(int[] nums, int k) {

if(nums == null || k <= 0) return new int [0];


int[] result = new int[nums.length - k + 1];
int max = Integer.MIN_VALUE;
int j = 0;
int prevMaxIndex =0;
for(int i = 0; i < nums.length - k + 1; i++){
for(; j < i + k; j++) {
if (nums[j] > max) {
max = nums[j];
prevMaxIndex = j;
}
}
result[i] = max;
if (prevMaxIndex >= i+1) {
j = i + 1 + k -1;
} else {
max = Integer.MIN_VALUE;
j = i+1;
}
https://admin.exam.chitkara.edu.in/test/preview?test_id=5b1ffa62-7e65-4864-8951-6872d9906a74&branch_id=7a4955c1-ae99-4872-9896-2126f0b…
} 27/46
4/29/2019 return result; CU Exams
}

Footer

public static void main(String[] args) {


Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int arr[]=new int[n];
for(int i=0;i<n;i++){
arr[i]=sc.nextInt();
}
int k=sc.nextInt();
int sol[]=maxSlidingWindow(arr,k);
for(int i=0;i<sol.length;i++){
System.out.println(sol[i]);
}
}
}

Q4 Test Case

Input Output

1 NO
3 4 NO
5 6 7 1 4 5 2 YES
NO

Weightage - 20

Input Output

2 YES
1 2 NO
3 3 5 NO
3 3 NO

Weightage - 20

Input Output

1 YES
4 6 YES
11 12 5 7 11 11 6 7 8 12 NO
YES

Weightage - 20

Input Output

2 NO
3 3 YES
6 7 11 10 6 7 YES
2 6 NO

Weightage - 20

Input Output
https://admin.exam.chitkara.edu.in/test/preview?test_id=5b1ffa62-7e65-4864-8951-6872d9906a74&branch_id=7a4955c1-ae99-4872-9896-2126f0b… 28/46
4/29/2019 CU Exams
3 NO
3 3 YES
6 7 11 10 6 7 YES
2 6 NO

Weightage - 20

Sample Input Sample Output

1 NO
2 3 NO
3 2 9 11 2 YES

Solution

Header

import java.util.*;
import java.lang.*;
import java.io.*;
public class file
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc=new Scanner(System.in);

//System.out.println("enter number of test cases");


int t=sc.nextInt();
for(int i=0;i<t;i++){
int inCount=sc.nextInt();
int outCount=sc.nextInt();
int in[]=new int[inCount];
int out[]=new int[outCount];
for(int j=0;j<inCount;j++){
in[j]=sc.nextInt();
}
for(int k=0;k<outCount;k++){
out[k]=sc.nextInt();
boolean flag=false;
for(int p=0;p<inCount;p++){
if(out[k]==in[p]){
System.out.println("YES");
flag=true;
break;
}
}
if(flag==false){
System.out.println("NO");
}
}

Footer

https://admin.exam.chitkara.edu.in/test/preview?test_id=5b1ffa62-7e65-4864-8951-6872d9906a74&branch_id=7a4955c1-ae99-4872-9896-2126f0b… 29/46
4/29/2019 } CU Exams
}

Q5 Test Case

Input Output

3 Hello world
Hello hello world World My name is Anand
My mY name Name is is is is Anand As per the research
As as per per per the research research

Weightage - 20

Input Output

3 Goodbye bye pal


Goodbye bye bye pal pal pal pal You are very slow
You are are very very very slow slow I like games
I i like like games

Weightage - 20

Input Output

3 I love to play and I also love to dance


I i love love to play play and I also love love to Hello there how are you
Hello there there how how are you It's too hot

Weightage - 20

Input Output

2 Goodbye bye pal


Goodbye bye bye pal pal pal pal You are very slow
You are are very very very slow slow

Weightage - 20

Input Output

5 Goodbye bye pal


Goodbye bye bye pal pal pal pal Hello world
Hello world You are very slow
You are are very very very slow slow I like games

Weightage - 20

Sample Input Sample Output

5 Good bye world


Good bye bye world world world Sam went to his business
Sam went went to his his his business Reya is the best player
Reya is is the the best best player Hello

Solution

Header

https://admin.exam.chitkara.edu.in/test/preview?test_id=5b1ffa62-7e65-4864-8951-6872d9906a74&branch_id=7a4955c1-ae99-4872-9896-2126f0b… 30/46
4/29/2019 CU Exams
import java.util.*;
public class file{
public static void main(String args[]){
Scanner in = new Scanner(System.in);

int numSentences = Integer.parseInt(in.nextLine());


while (numSentences-- > 0) {
String input = in.nextLine();
String output="", s;
String w[]=input.split(" ");
int l,i=1;
l=w.length;
s=w[0];
output=s;
while(i<l){
while(i<l && w[i].equalsIgnoreCase(s)){i++;}
if(i<l){
s=w[i];
output = output + " " + s;
}
i++;
}
System.out.println(output);
}

Footer

}
}

Q6 Test Case

Input Output

0 ERROR!
1
1 1

Weightage - 20

Input Output

4 75
4 12 13 14 15
3 56 57 53
5 38 49 50 63 75

Weightage - 20

Input Output

2 ERROR!
2 34 23
3 45 54 65
1
https://admin.exam.chitkara.edu.in/test/preview?test_id=5b1ffa62-7e65-4864-8951-6872d9906a74&branch_id=7a4955c1-ae99-4872-9896-2126f0b… 31/46
4/29/2019 CU Exams
Weightage - 20

Input Output

4 75
4 12 13 14 15 12
3 56 57 53
5 38 49 50 63 75

Weightage - 20

Input Output

3 12
4 12 13 14 15 57
3 56 57 53 50
5 38 49 50 63 75
Weightage - 20

Sample Input Sample Output

5 74
5 41 77 74 22 44 52
1 12 37
4 37 34 36 52 ERROR!
Solution

Header

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;

public class file {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

int numLines = Integer.parseInt(sc.nextLine());


ArrayList<ArrayList> listArray = new ArrayList<ArrayList>();
for(int i = 0;i<numLines;i++){
int numOfIntegers = sc.nextInt();
ArrayList<Integer> intArrayList = new ArrayList<Integer>();
for(int j=0;j<numOfIntegers;j++){
intArrayList.add(new Integer(sc.nextInt()));
}
listArray.add(intArrayList);
sc.nextLine();
}
int numQueries = Integer.parseInt(sc.nextLine());
for(int i=0;i<numQueries;i++){
int x = sc.nextInt()-1;
int y = sc.nextInt()-1;
// sc.nextLine();
if(x<listArray.size() && y<listArray.get(x).size()){
System.out.println(listArray.get(x).get(y));
https://admin.exam.chitkara.edu.in/test/preview?test_id=5b1ffa62-7e65-4864-8951-6872d9906a74&branch_id=7a4955c1-ae99-4872-9896-2126f0b…
} 32/46
}
4/29/2019 CU Exams
else{
System.out.println("ERROR!");
}
}

Footer

}
}

Q7 Test Case

Input Output

ILOVEINDIA EIND
4 VEIN

Weightage - 20

Input Output

LAZYDOGJUMPSOVERTHEFOX AZYDOGJUMP
10 ZYDOGJUMPS

Weightage - 20

Input Output

ONE E
1 O

Weightage - 20

Input Output

AAAAAAAAAAABBBBBBBBBBB AAAAAA
6 BBBBBB

Weightage - 20

Input Output

entersampleinput ample
5 tersa

Weightage - 20

Sample Input Sample Output

https://admin.exam.chitkara.edu.in/test/preview?test_id=5b1ffa62-7e65-4864-8951-6872d9906a74&branch_id=7a4955c1-ae99-4872-9896-2126f0b… 33/46
4/29/2019 CU Exams
ABCXYZ ABC
3 XYZ

Solution

Header

import java.util.*;

public class file {


public static String getSmallestAndLargest(String s, int k)
{

String substring = s.substring(0, k);


String smallest = substring;
String largest = substring;

for (int i = 1; i <= s.length() - k; i++) {


// Create a substring of length 'k'
substring = s.substring(i, i + k);

// If current substring is lexicographically smaller than 'smallest'


if (substring.compareTo(smallest) < 0) {
smallest = substring;
}

// If current substring is lexicographically larger than 'largest'


if (substring.compareTo(largest) > 0) {
largest = substring;
}
}

return smallest + "\n" + largest;


}

Footer

public static void main(String[] args) {


Scanner scan = new Scanner(System.in);
String s = scan.next();
int k = scan.nextInt();
scan.close();

System.out.println(getSmallestAndLargest(s, k));
}
}

Q8 Test Case

Input Output

https://admin.exam.chitkara.edu.in/test/preview?test_id=5b1ffa62-7e65-4864-8951-6872d9906a74&branch_id=7a4955c1-ae99-4872-9896-2126f0b… 34/46
4/29/2019 CU Exams
5 181
9 8 6 7 5

Weightage - 30

Input Output

4 71
5 4 7 6

Weightage - 30

Input Output

6 151
9 7 1 5 2 6

Weightage - 40

Sample Input Sample Output

5 57
5 3 4 2 1

Solution

Header

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class file {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);

int n = in.nextInt();
int[] calories = new int[n];
for(int k=0; k< n; k++){
calories[k] = in.nextInt();
}
Arrays.sort(calories);
long sum = 0;
for(int i = n-1; i>=0; i--) {
sum += (long)calories[i]*Math.pow(2, (n-1-i));
}
System.out.println(sum);

Footer

https://admin.exam.chitkara.edu.in/test/preview?test_id=5b1ffa62-7e65-4864-8951-6872d9906a74&branch_id=7a4955c1-ae99-4872-9896-2126f0b… 35/46
4/29/2019 CU Exams
}
}

Q9 Test Case

Input Output

1 8
2 3
1 1 -1
1 1 1

Weightage - 20

Input Output

1 2
3 2
1 0
0 1

Weightage - 20

Input Output

1 8
3 3
2 2 2
1 1 1

Weightage - 30

Input Output

1
2 3 Execution Timed Out
2 2
1 1

Weightage - 30

Sample Input Sample Output

1 8
3 4
2 3 3 2
2 1 1 1

Sample Input Sample Output

1 0
2 2
-1 1
0 1

Solution

Header

import java.io.OutputStream;

https://admin.exam.chitkara.edu.in/test/preview?test_id=5b1ffa62-7e65-4864-8951-6872d9906a74&branch_id=7a4955c1-ae99-4872-9896-2126f0b… 36/46
import java.io.InputStream;
po t ja
import a. o. putSt ea ;
java.io.InputStream;
4/29/2019 import java.io.PrintWriter; CU Exams
import java.util.Arrays;

import java.io.BufferedInputStream;
import java.util.ArrayList;

public class file {


static class TreasureHunt {
int mod = (int) (1e9 + 7);

public void solve(int testNumber, ScanReader in, PrintWriter out) {

int t = in.scanInt();
while (t-- > 0) {
int n = in.scanInt();
int m = in.scanInt();
int arr[][] = new int[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
arr[i][j] = in.scanInt();
}
}

ArrayList<int[]> arrayList = new ArrayList<>();


for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (arr[i][j] != -1) {
int[] b = new int[n * m + 1];
if (i - 1 >= 0) b[(((i - 1) * m) + j)] = 1;
if (i + 1 < n) b[(((i + 1) * m) + j)] = 1;
if (j - 1 >= 0) b[(((i) * m) + j - 1)] = 1;
if (j + 1 < m) b[((i) * m) + j + 1] = 1;
if (arr[i][j] == 1) b[n * m] = 1;
arrayList.add(b);
}
}
}
if (arrayList.size() == 0) {
out.println(CodeX.power(2, n * m, mod));
} else {
out.println(gauss(arrayList));
}
}

int gauss(ArrayList<int[]> a) {
int n = (int) a.size();
int m = (int) a.get(0).length;

int[] where = new int[m];


Arrays.fill(where, -1);
for (int col = 0, row = 0; col < m && row < n; ++col) {
int sel = row;
for (int i = row; i < n; ++i)
if (a.get(i)[col] == 1) {
sel = i;
break;
}

https://admin.exam.chitkara.edu.in/test/preview?test_id=5b1ffa62-7e65-4864-8951-6872d9906a74&branch_id=7a4955c1-ae99-4872-9896-2126f0b… 37/46
for (int i = 0; i < a.get(sel).length; ++i) {
o (
for t i
(int 0; i < a.get(se ). e gt ; ++i)
a.get(sel).length; ) {
4/29/2019 int temp = a.get(sel)[i]; CU Exams
a.get(sel)[i] = a.get(row)[i];
a.get(row)[i] = temp;
}

if (a.get(row)[col] == 0) continue;
where[col] = row;
for (int i = 0; i < n; ++i) {
if (i != row && a.get(i)[col] == 1) {
for (int k = 0; k < a.get(i).length; k++) {
a.get(i)[k] ^= a.get(row)[k];
}
}
}
++row;
}

if (where[m - 1] != -1) return 0;


int ans = 1;
for (int i = 0; i < m - 1; i++) {
if (where[i] == -1) ans = (ans * 2) % mod;
}
return ans;
}

static class ScanReader {


private byte[] buf = new byte[4 * 1024];
private int INDEX;
private BufferedInputStream in;
private int TOTAL;

public ScanReader(InputStream inputStream) {


in = new BufferedInputStream(inputStream);
}

private int scan() {


if (INDEX >= TOTAL) {
INDEX = 0;
try {
TOTAL = in.read(buf);
} catch (Exception e) {
e.printStackTrace();
}
if (TOTAL <= 0) return -1;
}
return buf[INDEX++];
}

public int scanInt() {


int I = 0;
int n = scan();
while (isWhiteSpace(n)) n = scan();
int neg = 1;
if (n == '-') {
neg = -1;
n = scan();
}
while (!isWhiteSpace(n)) {
if (n >= '0' && n <= '9') {

https://admin.exam.chitkara.edu.in/test/preview?test_id=5b1ffa62-7e65-4864-8951-6872d9906a74&branch_id=7a4955c1-ae99-4872-9896-2126f0b… 38/46
I *= 10;
I 0;
10;
4/29/2019 I += n - '0'; CU Exams
n = scan();
}
}
return neg * I;
}

private boolean isWhiteSpace(int n) {


if (n == ' ' || n == '\n' || n == '\r' || n == '\t' || n == -1) return true;
else return false;
}

static class CodeX {


public static int power(int x, int y, int p) {
int res = 1;
x = x % p;
while (y > 0) {
if ((y & 1) != 0)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}

Footer

public static void main(String[] args) {


InputStream inputStream = System.in;
OutputStream outputStream = System.out;
ScanReader in = new ScanReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TreasureHunt solver = new TreasureHunt();
solver.solve(1, in, out);
out.close();
}

Q10 Test Case

Input Output

5 1 puran 3200
ram 2 ram 2000
2000 3 raman 3100
sham 4 sham 2100

Weightage - 40

Input Output

6 1 taran 2100
pam 2 jam 2000
https://admin.exam.chitkara.edu.in/test/preview?test_id=5b1ffa62-7e65-4864-8951-6872d9906a74&branch_id=7a4955c1-ae99-4872-9896-2126f0b…
1000 3 shweta 1500 39/46
4/29/2019 jam CU4 Exams
kawal 1400
Weightage - 30

Input Output

5 1 deep 2100
kamal 2 kamal 1500
1500 3 preet 1000
samar 4 samar 1000

Weightage - 30

Sample Input Sample Output

5 1 Kanika 3500
Samar 2 Gopal 2700
2500 3 Sara 2600
Tarun 4 Samar 2500

Solution

import java.io.*;
import java.util.*;
import java.lang.*;
class File{
public static void main(String[] args) throws IOException{
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
List<Employee> l1=new LinkedList<Employee>();
int siz=Integer.parseInt(reader.readLine());
for(int i=0;i<siz;i++) {
String name=reader.readLine();
int SalaryIncrement=Integer.parseInt(reader.readLine());
l1.add(new Employee(name, SalaryIncrement));
}
String choice=reader.readLine();
if(choice.equals("SalaryIncrementSort"))
Collections.sort(l1,new SortBySalary());
else if(choice.equals("NameSort")) {
Collections.sort(l1,new SortByName());
}
Iterator <Employee>it=l1.iterator();
int i=1;
while(it.hasNext())
{
Employee obj=(Employee)it.next();
System.out.println(i++ +" "+obj.name+" "+obj.SalaryIncrement);
}
}
}

class Employee{
int SalaryIncrement;
String name;
Employee(String name,int SalaryIncrement){
this.SalaryIncrement=SalaryIncrement;
this.name=name;
}
public String toString(){
return this.name+" "+this.SalaryIncrement;
}

https://admin.exam.chitkara.edu.in/test/preview?test_id=5b1ffa62-7e65-4864-8951-6872d9906a74&branch_id=7a4955c1-ae99-4872-9896-2126f0b…
} 40/46
4/29/2019 class SortBySalary implements Comparator<Employee>{ CU Exams
public int compare(Employee n1,Employee n2){
// if(n1.SalaryIncrement==n2.SalaryIncrement)
// return (n1.name.compareTo(n2.name));
// else
return -(n1.SalaryIncrement-n2.SalaryIncrement);
}
}
class SortByName implements Comparator<Employee>{
public int compare(Employee n1,Employee n2) {
// if((n1.name).equals(n2.name))
// return -(n1.SalaryIncrement-n2.SalaryIncrement);
// else
return ( n1.name.compareTo(n2.name));
}
}

Q11 Test Case

Input Output

5 kripa 150
arpit 100 arpit 100
pinku 100 pinku 100
pinku 50 ayesha 75

Weightage - 25

Input Output

5 a 150
a 100 a 100
b 100 b 100
c 50 a 75

Weightage - 25

Input Output

3 a 150
a 100 a 100
a 75 a 75
a 150

Weightage - 25

Input Output

3 c 99
q 99 q 99
w 99 w 99
c 99

Weightage - 25

Sample Input Sample Output

5 aleksa 150
amy 100 amy 100
david 100 david 100
heraldo 50 aakansha 75
https://admin.exam.chitkara.edu.in/test/preview?test_id=5b1ffa62-7e65-4864-8951-6872d9906a74&branch_id=7a4955c1-ae99-4872-9896-2126f0b… 41/46
4/29/2019 CU Exams
Solution

Header

import java.util.*;

class Checker implements Comparator<Player>{

public int compare(Player a, Player b) {


// If 2 Players have the same score
if(a.score == b.score){
// Order alphabetically by name
if(a.name.compareTo(b.name) < 0){
return -1;
}
else if(a.name.compareTo(b.name) > 0){
return 1;
}
return 0;
}

// Otherwise, order higher score first


else if(a.score > b.score){
return -1;
}
else if(a.score < b.score){
return 1;
}

return 0;
}
}

Footer

class Player{
String name;
int score;

Player(String name, int score){


this.name = name;
this.score = score;
}
}

class Solution {

public static void main(String[] args) {


Scanner scan = new Scanner(System.in);
int n = scan.nextInt();

Player[] player = new Player[n];


Checker checker = new Checker();

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


player[i] = new Player(scan.next(), scan.nextInt());
}
scan.close();
https://admin.exam.chitkara.edu.in/test/preview?test_id=5b1ffa62-7e65-4864-8951-6872d9906a74&branch_id=7a4955c1-ae99-4872-9896-2126f0b… 42/46
4/29/2019 CU Exams
Arrays.sort(player, checker);
for(int i = 0; i < player.length; i++){
System.out.printf("%s %s\n", player[i].name, player[i].score);
}
}
}

Q12 Test Case

Input Output

({()}) true

Weightage - 25

Input Output

()[} false

Weightage - 25

Input Output

[(])}}{{ false

Weightage - 25

Input Output

()[]{} true

Weightage - 25

Sample Input Sample Output

{}() true

Solution

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
import java.util.ArrayList;

https://admin.exam.chitkara.edu.in/test/preview?test_id=5b1ffa62-7e65-4864-8951-6872d9906a74&branch_id=7a4955c1-ae99-4872-9896-2126f0b… 43/46
4/29/2019 class File CU Exams
{
public static boolean isBallanced(String paranString)
{
List<String>closingParan = Arrays.asList("}", ")", "]");
boolean isBallanced = true;
ArrayList<String> stack = new ArrayList<String>();
if(paranString.length()>0){
for(int i = 0;i<paranString.length();i++){
String paran = paranString.substring(i,i+1);
if(stack.size() == 0 && closingParan.contains(paran)){
isBallanced=false;
break;
}else{
if(paran.equals("{") || paran.equals("(") || paran.equals("[")){
stack.add(paran);
}else{
String stackParan = stack.get(stack.size()-1);
if((stackParan.equals("{") && paran.equals("}")) || (stackParan.equals("(") && paran.equals(")")
{
stack.remove(stack.size()-1);
}else{
isBallanced = false;
break;
}
}
}
}
}
if(stack.size()>0) isBallanced = false;
return isBallanced;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
if(isBallanced(sc.nextLine())){
System.out.println("true");
}else{
System.out.println("false");
}

}
}

Q13 Test Case

Input Output

5 Enter Array SizeEnter the Elements


0 First two Largest two numbers are:0 and 0
0 Last two Largetst two Numbers are:0and0
0

Weightage - 20

Input Output

5 Enter Array SizeEnter the Elements


1 First two Largest two numbers are:5 and 4
2 Last two Largetst two Numbers are:2and1
3

Weightage - 20
https://admin.exam.chitkara.edu.in/test/preview?test_id=5b1ffa62-7e65-4864-8951-6872d9906a74&branch_id=7a4955c1-ae99-4872-9896-2126f0b… 44/46
4/29/2019 CU Exams
Input Output

5 Enter Array SizeEnter the Elements


-1 First two Largest two numbers are:5 and 4
-2 Last two Largetst two Numbers are:-1and-2
0
Weightage - 40

Input Output

5 Enter Array SizeEnter the Elements


-1 First two Largest two numbers are:-1 and -2
-4 Last two Largetst two Numbers are:-4and-6
2

Weightage - 20

Sample Input Sample Output

10 Enter Array SizeEnter the Elements


9 First two Largest two numbers are:9 and 8
8 Last two Largetst two Numbers are:1and0
7

Solution

import java.util.Scanner;
public class file
{
public static void main(String[] args)
{
int n, l1, l2, temp;
Scanner s = new Scanner(System.in);
System.out.print("Enter Array Size");
n = s.nextInt();
if(n > 1)
{
int a[] = new int[n];
System.out.println("Enter the Elements");
for(int i = 0; i < n ; i++)
{
a[i] = s.nextInt();
}
for(int i = 0; i < n; i++)
{
for(int j = i + 1; j < n; j++)
{
if(a[i] < a[j])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
System.out.println("First two Largest two numbers are:"+a[0]+" and "+a[1]);
System.out.println("Last two Largetst two Numbers are:"+a[n-2]+"and"+a[n-1]);
}
else
{

System.out.println("Enter number greater than 1");


https://admin.exam.chitkara.edu.in/test/preview?test_id=5b1ffa62-7e65-4864-8951-6872d9906a74&branch_id=7a4955c1-ae99-4872-9896-2126f0b… 45/46
}
4/29/2019 } CU Exams
}
}

https://admin.exam.chitkara.edu.in/test/preview?test_id=5b1ffa62-7e65-4864-8951-6872d9906a74&branch_id=7a4955c1-ae99-4872-9896-2126f0b… 46/46

Das könnte Ihnen auch gefallen