Programming In Java | Week 7

Programming In Java Week 7 Answers NPTEL

Quiz

Q1. Which of these is a type of IO stream in Java?
a. Integer stream
b. Short stream
c. Byte stream
d. Character stream

Q2. Which of the following is a class in java.io package?
a. FileReader
b. ObjectInput
c. ObjectOutput
d. Datalnput

Q3. Consider the following program.
What will be the output if the above program is executed?

a. It will give compile-time error.
b. B
c. 66
d. r

Q4. Consider the following program.
What is the output of the above code?

a. java/programm/2023
b. java’programm/
c. java
d. 2023

Q5. Which method is used to read b length bytes from the input stream into an array?
a. public void read(int b)throws IOException { {
b. public int read(byte[ ] b)throws IOException {}
c. public void read(byte[ ] b)throws IOException {}
d. public int read(int b)throws IOException {}

Q6. How many standard streams Java can support?
a. 2
b. 3
c 4
d. 1

Q7. Which of the following stream is different from others?
a. System.in
b. System.out
c. PrintStream
d. FileOutputStream

Q8. Which of the following is used to read a string from the input stream?
a. get()
b. readLine( )
c. getLine( )
d. read()

Q9. Which of the following class(es) can be used for handling files in Java?
a. java files
b. java.io.File
c. java.io
d. java Filehandling

Q10. Which of the following statement(s) is/are true?
a. The default delimiters for a Scanner object are the white space characters.
b. The Scanner class has instance methods for reading each of the Java primitive types except char.

c. The Scanner methods do not throw any checked exceptions.
d. The Scanner class can be found in the java.util package.

Assignment

1. A byte char array is initialized. You have to enter an index value”n”. According to index your program will print the byte and its corresponding char value.
Complete the code segment to catch the exception in the following, if any. On the occurrence of such an exception, your program should print “exception occur” .If there is no such exception, it will print the required output.

Solution:

String s2 = new String(barr,n,1);
System.out.println(barr[n]);
System.out.println(s2);
}

2. The following program reads a string from the keyboard and is stored in the String variable “s1”. You have to complete the program so that it should should print the number of vowels in s1 . If your input data doesn’t have any vowel it will print “0”.

Solution:

for(int i=0;i<s1.length();i++){  
          char s2=s1.charAt(i);
          if(s2=='e' || s2=='E'|| s2=='a' || s2=='A' || s2=='i' || s2=='I' || s2=='o' || s2=='O' || s2=='u' || s2=='U') 
	     {
	       c=c+1;
             }    
         }

3. A string “s1” is already initialized. You have to read the index “n”  from the keyboard. Complete the code segment to catch the exception in the following, if any. On the occurrence of such an exception, your program should print “exception occur” .If there is no such exception, your program should replace the char “a” at the index value “n” of the “s1” ,then it will print the modified string.

Solution:

byte[] barr=s1.getBytes();
byte b1 = (byte) c;
barr[n]=b1;
System.out.println(new String(barr));
}

4. Complete the following code fragment to read three integer values from the keyboard and find the sum of the values. Declare a variable “sum” of type int and store the result in it.

Solution:

import java.util.*;
public class Question1
{
  public static void main (String[] args)
  {
    int i,n=0,sum=0;
    Scanner inr = new Scanner(System.in);
    for (i=0;i<3;i++)
    {
      n = inr.nextInt();
      sum =sum+n;
    }

5. Complete the code segment to catch the exception in the following, if any. On the occurrence of such an exception, your program should print “Please enter valid data” .If there is no such exception, it will print the “square of the number”.

Solution:

try{
  InputStreamReader r=new InputStreamReader(System.in);
  BufferedReader br=new BufferedReader(r);
  String number=br.readLine();
  int x = Integer.parseInt(number);
  System.out.println(x*x);
}
catch (Exception e)
{
  System.out.println("Please enter valid data");
}

* The material and content uploaded on this website are for general information and reference purposes only and don’t copy the answers of this website to any other domain without any permission or else copyright abuse will be in action.

Please do it by your own first!

DMCA.com Protection Status

5 1 vote
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
close button
0
Would love your thoughts, please comment.x
()
x