BSC Sem4 Java Programming Assignment 1 Ans
BSC Sem4 Java Programming Assignment 1 Ans
Assignment: TA (Compulsory)
1. The name of a Java program file must match the name of the class with the extension.
Java
A. True
2. Two methods cannot have the same name in Java.
A. True
3. The modulus operator (%) can be used only with integer operands
A. True
4. Declarations can appear anywhere in the body of a Java method.
A. True
5. All the bit wise operators have the same level of precedence in Java.
B. False
6. When X is a positive number, the operations x>>2 and x>>>2 both produce the same
result.
B. False
7. If a=10 and b=15, then the statement x=(a>b)?a:b; assigns the value 15 to x
A. True
8. In evaluating a logical expression of type
Boolean expression1 && Boolean expression2
Both the Boolean expressions are not always evaluated.
B. False
9. In evaluating the expression (x == y && a<b) the boolean expression x==y is evaluated
first and then a<b is evaluated.
A. True
10. The default base is always required in the switch selection structure.
B. False
11. The break statement is required in the default case of a switch selection structure.
B. False
12. The expression (x ==y && a<b) is true if either x == y is true or a<b is true
B. False
13. A variable declared inside the for loop control cannot be referenced outside the loop
A. True
Assignment: TB (Compulsory)
1. Why is Java known as platform-neutral language & how is Java more secured than other
languages?
3. List the eight basic data types used in Java. Give examples.
The eight basics data types used in Java are Integer, Floating point, Character and Boolean
which are primitive and classes, Interfaces, Arrays which are derived or non – primitive.
The process of converting one data type to another is called type casting. If operations are
needed to be performed on two operands of different data types then type casting required.
a. The ‘while’ loop contains a condition and a loop body. The control enters the loop
only after the condition is evaluated and returns the result as true. Therefore the
‘while loop’ is called as the top – tested loop. Whereas in the ’do.. While’ loop, the
loop is executed at least once regardless of whether the condition evaluates to true
or false. Only after the loop is executed once the condition is evaluated, therefore
the ‘do.. While’ is called as the bottom tested loop.
Syntax : while(test condition) do{
{ loop body
body of the loop; }
} while(condition);
b. The ‘while’ loop contains a condition and a loop body. The control enters the loop
only after the condition is evaluated and it returns the result as true. Therefore the
‘while loop’ is called as the top – tested loop. The ‘for’ statement is used to
manage iterative actions as an efficient alternative to the while statement. The ‘for’
statement creates a loop in which a set of statements is repeatedly executed until
the specified condition becomes false.
Syntax : while(test condition)
{
body of the loop;
}
for(initialization condition; test condition; update
expression)
{
statement 1;
statement 2;
}
c. break and continue statement – The continue statement stops the current
iteration of the loop and immediately starts the next iteration of the same loop.
When the current iteration of a loop stops, the statements after the continue
statement in the loop are not executed. Whereas by using the ’break’ statement we
can enforce the immediate termination of the loop by passing the conditional
expression and any remaining code in the body of the loop. Break statement is
encountered inside the loop, the loop is terminated and the program control
resumes the next statement following the loop.
Kuvempu University
Laboratory Assignment 1
(Academic Year 2005 - II Cycle)
3. Using switch and case statements write a program to add, subtract, multiply and divide the two
numbers.
import java.lang.*;
import java.math.*;
import java.io.*;
public class Java program
{
int x,y,z;
public Java program()
{
try
{
system.out.println(“Enter the First number”);
BufferdReaderbr=new BufferedReader(new InputStreamReader
(system. in));
x=Integer.ParseInt(br.readline());
system.out.println(“Enter the second number”);
y=Integer.ParseInt(br.readline());
switch(w0
{
case 1: z=x+y;
system.out.println(“The sum of the two numbers is”+z);
break;
case 2: z=x-y;
system.out.println(“The difference between the two
numbers is”+z);
break;
case 3: z=x*y;
system.out.println(“The product of the two numbers
is”+z);
break;
case 4: z=x/y;
system.out.println(“The division of the two numbers
is”+z);
break;
}
}
catch(Exception e)
{
system.out.println(“error”);
}
}
public static void main(String args[])
{
Java program j program;
J program=new Java program();
}
}
4. Using for loop write a program to add the integers up to 50 and print the result on the screen.
import java.math.*;
import java.lang.*;
public class integer
{
int m,n,x,;
public integer()
{
x=0;
for(m=1;m<=50;m++)
{
x=m+x;
}
system.out.println(“The sum of integers up to 50 is:”+x);
}
public static void main(String args[])
{
integer add;
add=new integer();
}
}