0% found this document useful (0 votes)
47 views

Assignment 4

This document contains an assignment for a programming fundamentals module. It includes 23 multiple choice and code writing questions covering Java programming concepts like conditional statements, operators, loops, methods and more. Students are asked to write code snippets, analyze code examples, predict output, and identify valid code insertions.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views

Assignment 4

This document contains an assignment for a programming fundamentals module. It includes 23 multiple choice and code writing questions covering Java programming concepts like conditional statements, operators, loops, methods and more. Students are asked to write code snippets, analyze code examples, predict output, and identify valid code insertions.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Institute of Software Engineering

Diploma in Comprehensive Master Java Developer (CMJD)

Batch – CMJD 105


Module – Programming Fundamentals
Assignment - 04
2 | Programming Fundamentals – Assignment 04

1. Input two numbers and check whether the first number is greater than the second number. If so
add the two numbers otherwise display the two numbers.

2. Write a java program to find the absolute number of a given integer number.

3. Enter marks Obtained by a student for Chemistry, Physics and Combined math. Calculate the total
and average. If the average is greater than 75 then display “Pass” otherwise “Fail”. Write a java
program to perform the above task.

4. Enter the unit price and amount bought from a product. Calculate the total. If the total is greater
than Rs.1500/- display “You are entitled to the super draw. Otherwise display “try again”.

5. Enter unit price and amount bought from a product. Calculate the total. If the total is more than
Rs.500/- give 5% discount. Calculate the discount and new total and display those. Otherwise
display “No discount given”.

6. Write a Java program to get a Year from user input and find whether it is a leap year or not.

7. Write a Java program to find & print the area of a circle when user input the radius.

8. Customers can withdraw cash from an ATM. Withdrawal is refused


if amount entered>current balance
if amount entered > daily limit
if current balance < 5000 rupees, then a charge of 2% is made.
if current balance >= 5000, no charge is made
Write a java program which input a request for a sum of money decides if a withdrawal can be
made or not and calculate any charges. Appropriate output messages should be included.

9. Write a java program to find the maximum number of three integer numbers input by the
keyboard and print results as follows; “Maximum number is : 45”

10. Write a Java program to input an integer number from the keyboard and print whether the
number is odd or even.
11. Which of the following lines can be legally inserted at line 10?
class Example{
public static void main(String[] args) {
int x = 10;
//Insert code here//Line 10
}
}
3 | Programming Fundamentals – Assignment 04

A. if(x){} B. if(x=10){}
C. if(x==10){} D. if(x=100!=10){}
E. if((x=100)!=10){} F. if((x=100)>0==true){}

12. Which of the following lines can be legally inserted at line 10?
class Example{
public static void main(String[] args) {
int x = 10;
boolean b=true;
//Insert code here//Line 10
}
}
A. if(b){} B. if(b=false){}
C. if(b==false){} D. if(b=false==false){}
E. if((b=false)==false){}
F. if(b=(false==true)){}

13. Write the outputs for the following code lines.


int x = 10;
A System.out.println(x=9);
B. System.out.println(x==9);
C. System.out.println(x=9!=10);
D. System.out.println((x=9)==10);
E. System.out.println((x=9)<=10);

14. Write the outputs for the following code lines.


boolean b=true;
A. System.out.println(b);
B. System.out.println(b=true);
C. System.out.println(b==true);
D. System.out.println(b!=true);
E. System.out.println(b=true==true);
F. System.out.println((b=true)==false);
G. System.out.println(b=(true!=false));

15. Write the outputs for the following code lines.


int x=99;
if(x++==x){
System.out.println("x++==x : "+x);
}
x=99;
4 | Programming Fundamentals – Assignment 04

if(++x==x){
System.out.println("++x==x : "+x);
}
x=99;
if(x==x++){
System.out.println("x==x++ : "+x);
}
x=99;
if(x==++x){
System.out.println("x==++x : "+x);
}
x=99;
if(++x==++x){
System.out.println("++x==++x : "+x);
}
x=99;
if(x++==x++){
System.out.println("x++==x++ : "+x);
}
x=99;
if(++x==x++){
System.out.println("++x==x++ : "+x);
}
x=99;
if(x++==++x){
System.out.println("x++==++x : "+x);
}

16. Write the outputs for the following code lines.


int x=99;
if(x++==x){
System.out.println("x++==x : "+x);
}
if(++x==x ){
System.out.println("++x==x : "+x);
}
if(x==x++){
System.out.println("x==x++ : "+x);
}
if(x==++x){
System.out.println("x==++x : "+x);
}
5 | Programming Fundamentals – Assignment 04

if(++x==++x){
System.out.println("++x==++x : "+x);
}
if(x++==x++){
System.out.println("x++==x++ : "+x);
}
if(++x==x++){
System.out.println("++x==x++ : "+x);
}

17. Write the outputs for the following code lines.


int x=99;
int y=99;
if(x++==y){
System.out.println("x++==y : "+x+" : "+y);
}
if(++x==y){
System.out.println("++x==y : "+x+" : "+y);
}
if(x==y++){
System.out.println("x==y++ : "+x+" : "+y);
}
if(x==++y){
System.out.println("x==++y : "+x+" : "+y);
}
if(++x==++y){
System.out.println("++x==++y : "+x+" : "+y);
}
if(x++==y++){
System.out.println("x++==y++ : "+x+" : "+y);
}
if(++x==y++){
System.out.println("++x==y++ : "+x+" : "+y);
}
if(x++==++y){
System.out.println("x++==++y : "+x+" : "+y);
}
6 | Programming Fundamentals – Assignment 04

18. Given code fragment:


int x=9;
/*Insert code here */ { //12
System.out.println("Success");
}
Which of the followings can be inserted at the line 12 to get the output “Success”
A. if(x>=10) B. if(x++>=10) C. if(++x>=10)
D. if(++x>=x++) E. if(++x>x++) F. if(x++>=x++)
G. if(++x<=x++) H. if(x<=x++)

19. Given code fragment:


int x=100,y=99;
/* Insert Code here*/ { //Line 12
System.out.println("Success");
}else{
System.out.println("Failed");
}
Which of the following lines can be inserted at the line 12 to get the output as “Success”
A. if(x==y) B. if(x++==++y)
C. if(x++==y++) D. if(++x==y++)

20. Assume that i = 1, j = 2, k = 3 and m = 2. What does each of the following statements print?
System.out.println( i ==1); //Line 1
System.out.println( j ==3); //Line 2
System.out.println( ( i >=1) && ( j <4) ); //Line 3
System.out.println( ( m <=99) & ( k < m ) ); //Line 4
System.out.println( ( j >= i ) || ( k == m ) ); //Line 5
System.out.println( ( k + m < j )|(3- j>= k)); //Line 6
System.out.println( !( k > m ) ); //Line 7

21. What are the outputs of the following commands?


int x=20,y=60;
boolean bool;
System.out.println(x=10); //Line 1
System.out.println(bool=true); //Line 2
System.out.println(x=10>0); //Line 3
System.out.println((x=10)>0); //Line 4
System.out.println(bool=(x=10)>0); //Line 5
System.out.println(bool=x+y>100); //Line 6
7 | Programming Fundamentals – Assignment 04

22. Given:
class Example{
public static void main(String args[]){
//line 5
switch(x){
default : System.out.print("4 ");
case 1 : System.out.print("1 ");
case 2 : System.out.print("2 ");
case 3 : System.out.print("3 ");
}
}
}
What will be the outputs when you insert the following codes at line 5?
A. int x=1; B. int x=2; C. int x=3;
D. int x=4; E. int x=0; F. int x=5;

23. Which of the following lines are legal?


import java.util.*;
class Example{
public static void main(String args[]){
int x=100;
System.out.println(x); //Line 1
{
int y=200;
{
int z=300;
System.out.println(x); //Line 2
System.out.println(y); //Line 3
System.out.println(z); //Line 4
}
System.out.println(x); //Line 5
System.out.println(y); //Line 6
System.out.println(z); //Line 7
}
System.out.println(x); //Line 8
System.out.println(y); //Line 9
System.out.println(z); //Line 10
}
}
A. Line 1 B. Line 2 C. Line 3 D. Line 4
E. Line 5 F. Line 6 G. Line 7 H. Line 8
I. Line 9 K. Line 10
8 | Programming Fundamentals – Assignment 04

24. Which of the following code fragments can be inserted at line 10 to legal line 12
final int y;
y=100;
int z=100;
int a;
//Insert code here //Line 10
System.out.println(a); //Line 12

A. if(x>0){a=0;} B. a=0; C. if(y>0){a=0;}


D. if(z>0){a=0;} E. if(true){a=0;} G. if(y>0){a=0;}else {a=-1;}
H. a=z>0?0:-1;

25. Given:
class Example {
public static void main(String[] args) {
int a = 2;
char b,c,d;
b = (a < 2) ? 'f' : 'g'; // 1
if (a < 2) c = 'h'; else c = 'i'; // 2
if (a < 2) d = 'j'; // 3
if (a > 2) d = 'k'; // 4
if (a == 2) d = 'l'; // 5
System.out.print(b + "," + c + "," + d); // 6
}
}
What is the result of attempting to compile and run the program?
A. Prints: g, i, l
B. Compiler Error: variable b might not have been initialized.
C. Compiler Error: variable c might not have been initialized.
D. Compiler Error: variable d might not have been initialized.
E. Runtime Exception
F. None of the above.

26. Given Code:


class Example{
public static void main(String args[]){
int a=-5;
int b=-2;
a%=b;
a/=b;
b=a>0?0:a;
System.out.println(a+" "+b);
9 | Programming Fundamentals – Assignment 04

}
}
Select one option?
A. Prints 10 B. Prints -1 -1
C. Prints -2 -2 D. Prints 0 0

27. Given Code fragment:


Scanner input=new Scanner(System.in);
System.out.print("Input student average : ");
double avg=input.nextDouble();
if(avg>=50){
System.out.println("Pass");
}else{
System.out.println("Fail");
}
System.out.println("Thanking you..");
What are outputs for the following inputs?
A. 99 B. 75 C. 49.99 D. 50.01 E.50.0 F. 49.0 G. 25

28. What are the outputs of the following commands?


System.out.println(12+8/5%4*(5-4/5)+4*5); //Line 1
System.out.println(4%5*3-4/7+4%2-5/(5*4%5)); //Line 2
System.out.println(5-8%4*5(5/8*(3%4)*4)+8/4+1); //Line 3
System.out.println(1.5%2.1-5.4*1.1/(5.4%5)); //Line 4
System.out.println((5+4)%4+(5/8.0)+4); //Line 5
System.out.println(5-4*6(5%4-3)*5+6/(1.0/2.0)-5*4); //Line 6
System.out.println(7+3-4*4%6+4*2.5-3%2); //Line 7
System.out.println(5-7*(9%4)+5+8/7+2); //Line 8
System.out.println((2-5%5)-10.8%5.1*5*4); //Line 9
System.out.println(5+5-4/(3%1+5+(7-8)*4+5)); //Line 10
System.out.println(9%4*5+6%10-5*4); //Line 12
System.out.println(9%1+5-(5+5%2)-5/8+5); //Line 13
System.out.print(((7 * 2) % 5)+" "); //Line 14
System.out.print(" " + (7 % 5)); //Line 15
10 | Programming Fundamentals – Assignment 04

29. What are the outputs of the following commands?


int a=1,b=2,c=3,d=4;
int x;
x=a++ + b++ + c++ + d++;
System.out.println(a+" "+b+" "+c+" "+d+" "+x); //Line 1
x+=a+=b+=c+=d; //Line 2
System.out.println(a+" "+b+" "+c+" "+d+" "+x); //Line 3
x=a=b=c=d; //Line 4
System.out.println(a+" "+b+" "+c+" "+d+" "+x); //Line 5

30. What are the outputs of the following commands?


boolean b = false;
System.out.println(10>4 && true !=b==(10+3/2==8)==true); //Line 1
System.out.println(b=false==true||true!=(b=false)); //Line 2
System.out.println((b=false)=true?4:5==5 & true==3*2<=29); //Line 3
System.out.print (('a' == 'a')+" "); //Line 4
System.out.print(('a' == 'b')+" "); //Line 5
System.out.print((5 != 6)+" "); //Line 6
System.out.print((5.0 == 5L)+" "); //Line 7
System.out.println((true == false)); //Line 8

31. What are the outputs of the following commands?

boolean b1, b2, b3;


b1 = true != false; //Line 1
b2 = 5%3 == 2 ^ true == !false ; //Line 2
System.out.println((b3 =true) & b2 || b1 == false); //Line 3
System.out.println(b3 = b2 == b1); //Line 4
b3= true; //Line 5
System.out.println(b3^b2&b1|false != (b3 = false)); //Line 6
System.out.println(!b3==b2 && b2 != b1 ||!b1 != b2); //Line 7

32. Given:
class Example{
public static void main(String args[]){
//line 5
switch(x){
default : System.out.print("4 ");
case 1 : System.out.print("1 ");
case 2 : System.out.print("2 ");
case 3 : System.out.print("3 ");
11 | Programming Fundamentals – Assignment 04

}
}
}
What will be the outputs when you insert the following codes at the line 5?
A. int x=1; B. int x=2; C. int x=3;
D. int x=4; E. int x=0; F. int x=5;

33. Given:
//Insert code here //line 4
switch(x){
case 'A' : System.out.println("65 ");break;
case 'B' : System.out.println("66 ");break;
case 'C' : System.out.println("67 ");break;
default : System.out.println("wrong ");
}
Which of the following codes can be inserted legally at line 4
A. char x=’A’; B. int x=65; C. int x=65536;
D. byte x=65; E. short x=66 ; F. boolean x =true;
G. String x=”A”; H. double x=65.0;

34. Given:

class Example{
public static void main(String args[]){
//Line 5
switch(x){
default : System.out.print("4 ");break;
case 2 : System.out.print("2 ");
case 3 : System.out.print("3 ");
case 1 : System.out.print("1 ");break;
}
}
}
What will be the output when you insert the following codes at line 5?
A. int x=1; B. int x=2; C. int x=3;
D. int x=4; E. int x=0; F. int x=5;

35. Given:
class Example{
public static void main(String args[]){
//line 5
switch(x){
12 | Programming Fundamentals – Assignment 04

default : System.out.print("4 ");


case 1 : System.out.print("1 ");
case 2 : System.out.print("2 ");
case 3 : System.out.print("3 ");
}
}
}

What will be the outputs when you insert the following codes at line 5?
A. int x=1; B. int x=2; C. int x=3;
D. int x=4; E. int x=0; F. int x=5;

You might also like