Sunteți pe pagina 1din 5

Kuvempu University

Assignment 1 for B.Sc. (IT) & M.Sc. (IT) Courses


(Academic Year 2005 - II Cycle)

Subject: JAVA Programming


Subject Code: BSIT - 42

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?

Java Programming contain no implementation – dependent aspects so the result of executing


a series of java byte codes should always be the same no matter on what system they are
executed. Java’s executable files are composed of byte codes that are instructions and data
relating to a hypothetical computer called Java Virtual Machine. This helps in executing
programs on any platform therefore it is known as Platform Independent Language.

2. What is the task of the main method in a Java program?


Link in C and C++ environment the programs written in Java are executed with a call to main
() method. The main () method is guides the interpreter in determining the position from
which byte code should be interpreted. A program can contain any number of classes but one of
them needs to contain the main method.

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.

4. What is type casting? Why is it required in programming?

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.

5. In what ways does a switch statement differ from an if statement?


If statement uses the result of a conditional statement to enter the loop and executes all the
statements inside the loop whereas the switch statement uses the result of the conditional
statement to jump to some location within the switch statement. The positions it can jump to
when the expression evaluates to the given constant are marked with case labels that take the
form : ‘case constants’. In the if statement all the statements inside the loop are executed
where as on the switch statement only the resultant case labels are executed.

6. Compare in terms of their functions, the following pairs of statements:


a. While and do……….while
b. While and for
c. Break and continue

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)

Subject: JAVA Programming


Subject Code: BSIT - 42

1. Write a program to print, “ Welcome to Java Programming” on the screen.


import java.lang.*;
public class Java program
{
public static void main(String args[])
{
system.out.println(“Welcome to Java Programming”);
}
}

2. Write a program to find the greatest of the given three numbers.


import java.lang.*;
import java.math.*;
public class Java program
{
public static void main(String args[])
{
int a=10;
int b=20;
int c=30;
if(a>b && a>c)
{
system.out.println(“a is the greatest number”);
}
if(c>b && c>a)
{
system.out.println(“c is the greatest number”);
}
else
{
system.out.println(“b is the greatest number”);
}
}
}

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());

system.out.println(“Enter 1 for addition, 2 for sub,3


for mult, 4 for div”);
w=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();
}
}

S-ar putea să vă placă și