Programming In Java | Week 12
Programming In Java Week 12 Answers NPTEL
Quiz
Q1. Which statement is incorrect in case of using “this” keyword in a static method?
a. “this” keyword can be used in static method to access static variables only
b. “this” keyword first needs to be defined by user
c. “this” keyword cannot be used as a variable name
d. “this” keyword can not be used in static method to access static variables only
Q2. State whether the following statements are True or False.
i) A catch can not have comma-separated multiple arguments.
ii) Throwing an Exception always causes program termination.
a. True. False
b. False. True
c. True. True
d. False. False
Q3. Which of the following contains only date and not time?
a. java.io.date
b. java.sql.date
c. java.util.date
d. java.util. dateTime
Q4. Why the applets are depreciated?
a. Applet are complicated to program
b. Applet had severe security issues
c. Applet was replaced by AWT
d. Applet was resource intensive
Q5. Which of the following control expressions are valid for an if statement?
a. Any integer expression.
b. 0 and 1 as integer.
c. A String object.
d. Any Boolean expression.
Q6. Consider the following program:
String animal = “GOAT”:
switch(animal){
break: System.out.println(“DOMESTIC”):
}
What is the output of the Java program given above?
a. No output
b. GOAT
c. DOMESTIC
d. Compiler error
Q7. Consider the following program:
What is the output of the following program?
a. java
b. ava
c. y java
d. ava n
Q8. Which of the following are correct statement for array declaration?
a. float[] = new float (3);
b. float f2[] = new floatl[];
c. float[] f1 = new float[3];
d. float F3[] = new float[3];
Q9. Consider the following piece of code in Java.
What is the result, if the above-mentioned program is executed?
a. finally
b. finally exception finally finished
c. finally exception finished
d. Compilation fails
Q10. Which is a component in AWT that can contain another components like buttons, textfields, labels ete.?
a. Window
b. Panel
c. Container
d. Frame
Assignment
1. Complete the code to develop an extended version of the ADVANCED CALCULATOR with added special functions that emulates all the functions of the GUI Calculator as shown in the image.
Note the following points carefully:
1. Use only double datatype to store all numeric values.
2. Each button on the calculator should be operated by typing the characters from ‘a’ to ‘t’.
3. You may use the already defined function gui_map(char).
4. Use predefined methods from java.lang.Math class wherever applicable.
5. Without ‘=’ binary operations won’t give output as shown in Input_3 and Output_3 example below.
5. The calculator should be able to perform required operations on one or two operands as shown in the below example:
Input_1:
okhid
Output_1:
100.0
Input_2:
ia
Output_2: 2.0
Solution:
outerloop:
for(int i=0; i<seq.length; i++)
{
int r=0;
if(seq[i]=='+'||seq[i]=='-'||seq[i]=='/'||seq[i]=='X'||seq[i]=='=')
{
for(int j=0; j<i; j++)
{
o1+=Character.toString(seq[j]);
}
operand1=Double.parseDouble(o1);
for(int k=i+1; k<seq.length; k++)
{
if(seq[k]=='=')
{
outflag=1;
operand2=Double.parseDouble(o2);
if(seq[i]=='+')
{
output=operand1+operand2;
}
else if(seq[i]=='-')
{
output=operand1-operand2;
}
else if(seq[i]=='/')
{
output=operand1/operand2;
}
else if(seq[i]=='X')
{
output=operand1*operand2;
}
break outerloop;
}
else
{
o2+=Character.toString(seq[k]);
}
}
}
else if(seq[i]=='R' || seq[i]=='S' || seq[i]=='F')
{
for (int j=0;j<i;j++)
o1+=Character.toString(seq[j]);
operand1=Double.parseDouble(o1);
if (seq[i]=='R')
System.out.print(Math.sqrt(operand1));
else if(seq[i]=='S')
System.out.print(operand1*operand1);
else if (seq[i]=='F')
System.out.print(1/operand1);
}
}
2. A partial code fragment is given. The URL class object is created in try block.You should write appropriate method( ) to print the protocol name and host name from the given url string.
For example:
https://www.xyz.com:1080/index.htm
protocol://host:port/filename
Solution:
try{
URL url=new URL("http://www.Nptel.com/java-tutorial");
System.out.println("Protocol: "+url.getProtocol());
System.out.println("Host Name: "+url.getHost());
}
catch(Exception e){System.out.println(e);}
}
}
3. Write a program to create a record by taking inputs using Scanner class as first name as string ,last name as string ,roll number as integer ,subject1 mark as float,subject2 mark as float. Your program should print in the format
“name rollnumber avgmark”.
For example:
input:
ram
das
123
25.5
24.5
output:
ramdas 123 25.0
Solution:
String f = s1.next();
String l = s1.next();
int n = s1.nextInt();
double db = s1.nextDouble();
double db1 = s1.nextDouble();
double avg=(db+db1)/2;
System.out.println(f + l +" "+ n +" "+avg );
4. A program code is given to call the parent class static method and instance method in derive class without creating object of parent class. You should write the appropriate code so that the program print the contents of static method() and instance method () of parent class.
Solution:
public static void main(String[] args)
{
Child c= new Child();
c.testInstanceMethod();
Parent.testClassMethod();
}
}
5. Write a recursive function to print the sum of first n odd integer numbers. The recursive function should have the prototype
” int sum_odd_n(int n) “.
For example :
input : 5
output: 25
input : 6
output : 36
Solution:
int c=n,sum=0;
if(c==n){
sum=n*2-1;
}
return sum+sum_odd_n(n-1);
* 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!
