0% found this document useful (0 votes)
5 views7 pages

Oops-Insem2-Anskey (2022)

The document contains a series of multiple-choice questions (MCQs) and descriptive questions (DES) related to Java programming concepts, including access specifiers, exception handling, file operations, and multithreading. Each question is accompanied by a point value, indicating its weight in an assessment. The document also includes example code snippets for exception handling and multithreading tasks.

Uploaded by

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

Oops-Insem2-Anskey (2022)

The document contains a series of multiple-choice questions (MCQs) and descriptive questions (DES) related to Java programming concepts, including access specifiers, exception handling, file operations, and multithreading. Each question is accompanied by a point value, indicating its weight in an assessment. The document also includes example code snippets for exception handling and multithreading tasks.

Uploaded by

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

Type: MCQ

Q1. Which of these access specifiers can be used for an interface? (0.5)
1. **public
2. protected
3. private
4. All of these

Q2. Which of these keywords is used by a class to use an interface defined previously? (0.5)
1. **implements
2. import
3. export
4. define
Q3. Which of these keywords must be used to monitor for exceptions? (0.5)

1. catch
2. **try
3. finally
4. throw

Q4. Which of these keywords is not a part of exception handling? (0.5)


1. try
2. finally
3. **thrown
4. Catch
Q5. What will be the output of the following code?

(0.5)
1. Exception
2. Exceptionhandled
3. **handled
4. No output

Q6. Which of these methods of Thread class is used to find out the priority given to a thread? (0.5)
1. ** getPriority()
2. getThreadPriority()
3. threadPriority()
4. priority()
Q7. Which function of Thread class is used to check weather current thread being checked is still
running? (0.5)
1. alive()
2. **isAlive()
3. isRunning()
4. run()
Q8. Which of these classes is/are used for input and output operation when working with bytes?
(0.5)
1. Reader
2. **InputStream
3. Writer
4. All of these
Q9. Which of the following classes is not related to input and output stream in terms of functioning?
(0.5)
1. Reader
2. **File
3. Writer
4. InputStream
Q10. Which of these is a process of writing the state of an object to a byte stream?
(0.5) 1. **Serialization

2. Deserialization
3. Externalization
4. File filter

Type: DES

Q11. Write a java program to input Registration number, city, average mark, email id of the student,
and validate the input data as per following criteria using exception handling technique:
i. Average mark must be between 0 and 100.
ii. Check for validity of email id (valid if ends with gmail.com or manipal.edu).
(2)
Scheme:
class StudData_Exception extends Exception //user defined exception class

public StudData_Exception(String s)

super(s);

class StudData_Exception_main

void avg_markCheck(int avg_marks) throws StudData_Exception{

if(avg_marks<0 || avg_marks>100)

throw new StudData_Exception("Avg.marks must be between 0 and 100 ");

else

System.out.println(" Avg. marks is Valid ");

void emailCheck(String email) throws StudData_Exception{

String em[]=email.split("@");

if(em[1].equalsIgnoreCase("gmail.com") ||em[1].equalsIgnoreCase("manipal.edu"))

System.out.println(" Email is Valid ");

else

throw new StudData_Exception("Email must be from gmail.com or manipal.edu


domain ");

public static void main(String args[])

StudData_Exception_main obj = new StudData_Exception_main();

try

obj.avg_markCheck(-10);

obj.emailCheck("[email protected]");

catch (StudData_Exception er)


{

System.out.println("Caught " +er);


}

Throwing the 1st exception(avg_mark validation) → 0.5M


Handling the 1st exception(avg_mark validation) → 0.5M
Throwing the 2nd exception(email-id validation) → 0.5M
Handling the 2nd exception(email-id validation) → 0.5M

Q12. The following program is used to copy the content of source.txt into dest.txt. Identify the
mistake if there is any, and suggest your solution.

(2)
Scheme:

Error in Line-12 : FileReader object (ie file_in) must be used to invoke read() method.
Error in Line-14: FileWriter object (ie file_out) must be used to invoke write() method.
Identification of mistake in Line-12 → 0.5M

Solution → 0.5M
Identification of mistake in Line-14 → 0.5M
Solution → 0.5M
Q13. Write a program to read all lines from the file “sample.txt” in class “ReadLine” and store them
in string “s1”. The method “process()” searches for string “s2” and replaces it with string “s3”. The
updated string “s1” should be stored in the file “updated.txt” through method “fileUpdate()”. The
main class “MainClass” creates objects and calls the methods to do tasks. Consider suitable
exceptions in the program. (3)
Scheme:
Reading file “sample.txt” → 0.5
Storing the contents of “sample.txt” in s1 → 0.5
Search for s2 in process() → 0.5
Replace s2 with s3→ 0.5
File update() → 0.5
Main class→ 0.5
Q14. Develop a multithreaded application in java to perform array operations as follows:
MainThread: Read input elements from the keyboard and store them in an array, also display
the count of odd and even numbers obtained from the following threads:
Thread-1: Display all the odd numbers present in the array.
Thread-2: Display all even numbers present in the array.
Note: Create the threads using Runnable interface to perform the above operations. (3)
Scheme:

import java.util.Scanner;
class MyThread implements Runnable
{
private int Thread_Num;
private int Arr[];
private int count = 0;
MyThread( int no , int A[] )
{
Thread_Num = no;
Arr = A;
}
public void run()
{
if( Thread_Num == 1)
display_even();
if( Thread_Num == 2)
display_odd();
}
public void display_even()
{
System.out.println("The even values are:");
for( int ele : Arr )
{
if( ele % 2 == 0 )
{ count++;
System.out.println(ele);
}
}
}

public void display_odd()


{
System.out.println("The odd values are:");
for( int ele : Arr )
{
if( ele %2 != 0 )
{
count++;
System.out.println(ele);
}
}
}
public int get_count()
{
return count;
}
}

class MultiThread_ArrDemo
{
public static void main(String[] args) throws Exception
{
System.out.println("Main thread starting.");
Scanner S = new Scanner(System.in);
System.out.println("Enter how many values:");
int size = S.nextInt();
int Arr[] = new int[size];
System.out.println("Enter the values:");
for( int i = 0 ; i < size ; i++)
Arr[i] = S.nextInt();

MyThread MT1 = new MyThread(1,Arr);


MyThread MT2 = new MyThread(2,Arr);

Thread T1 = new Thread( MT1 );


Thread T2 = new Thread( MT2 );

T1.start();
T1.join();
T2.start();
T2.join();

System.out.println("Even count: " +MT1.get_count() );


System.out.println("odd count: " +MT2.get_count() );
}
}

Proper creation of Runnable object & Thread object → 0.5M


Correct logic for task-1 by the 1st thread (generating odd numbers,count) → 0.5M
Correct logic for task-2 by the 2nd thread (generating even numbers,count) → 0.5M
Correct implementation of Thread join operation for thread-1 → 0.5M Correct
implementation of Thread join operation for thread-2 → 0.5M Displaying count of
odd and even numbers in the main thread → 0.5M

You might also like