Programming in Java NPTEL Assignment Answers of Week 7(2023)

In this post We are solving the Programming in Java NPTEL Assignment Answers of Week 7(2023)

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

Answer : c , d

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

Answer : a

3] Consider the following program.

import java.io.*;
   public class Question {
     public static void main (String [] args)
{
  try {
    PrintWriter writer = new Printwriter (System.out) ;
    writer. write (64+'2');
    writer. close ( ) ;
  }
  catch (Exception e) {
  System.out.println(e) ;
}
}
}

What will be the output if the above program is executed?
a] It will give compile-time error.

b] B

c] 66

d] r

Answer : d

4] Consider the following program.

import java. io.*;
   public class files
   {
     public static void main (String args[] )
     {
      File obj = new File ("java/ programm/2023") ;
      System. out. print (Obj . getName ( ) ) ;
     }
  }

What is the output of the above code?
a] java/programm/2023
b] java/programm/
c] java
d] 2023

Answer : d

5] 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 ] b)throws IOException{}
d] public int read(int b)throws IOException { }

Answer : b

6] How many standard streams Java can support?

a] 2

b] 3

c] 4

d] 1

Answer : b

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

Answer : d

8] Which of the following is used to read a string from the input stream?

a] get()
b] readLine( )
c] getLine( )
d] read( )

Answer : b

9] 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

Answer : b

10] 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.

Answer : a , b, d

Week 7 : Programming Assignment 1

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.

Code :

// Complete the code to get specific indexed byte value and its corresponding char value.
       if (n < 0 || n >= barr.length) {
                throw new IndexOutOfBoundsException();
            }
            
            System.out.println("Byte value: " + barr[n]);
            System.out.println("Corresponding char value: " + (char)barr[n]);
        } 

Week 7 : Programming Assignment 2

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”.

Code :

//Write your code here to count the number of vowels in the string "s1"

      s1 = s1.toLowerCase();
            
        // Count the number of vowels in the string
            for (int i = 0; i < s1.length(); i++) {
                char ch = s1.charAt(i);
                if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
                    c++;
                }
            }

Week 7 : Programming Assignment 3

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.

Code :

//Replace the char in string "s1" with the char 'a' at index "n"  and print the modified string 
char[] s1Chars = s1.toCharArray();
        s1Chars[n] = 'a';
            s1 = new String(s1Chars);

            // Print the modified string
            System.out.println(s1);
        } 

Week 7 : Programming Assignment 4

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.

Code :

// Write the appropriate statement(s) to import the package(s) you need in your program
import java.util.*;

public class Question1 { 
    public static void main(String[] args) {
        // Write the appropriate code to read the 3 integer values and find their sum.
        Scanner in = new Scanner(System.in);
        int num1 = in.nextInt();
        int num2 = in.nextInt();
        int num3 = in.nextInt();
        int sum = num1 + num2 + num3;

Week 7 : Programming Assignment 5

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”.

Code :

try {
            InputStreamReader r = new InputStreamReader(System.in);
            BufferedReader br = new BufferedReader(r);
            String number = br.readLine();
            int x = Integer.parseInt(number);
            System.out.print(x * x);
        } catch (Exception e) {
            // Print "Please enter valid data" if an exception is caught
            System.out.print("Please enter valid data");
      }

Leave a Reply

Your email address will not be published. Required fields are marked *