Você está na página 1de 11

NAME: ASHISH GHIMIRE

SUBJECT: PROGRAMMING
GROUP: N2
WEEKLY ASSIGNMENT 2
Q:1) wap to swap two numbers using temporary
variable.
Ans:
public class swap
{
void swap()
{
int x = 5;
int y = 8;
System.out.println("x =" + x);
System.out.println("y =" + y);
int temp;
temp = x;
x = y;
y = temp;
System.out.println("x="+x);
System.out.println("y="+y);
}
}

Q:2) wap to swap two numbers without using


temporary variable.
public class swap2
{
void swap()
{
int x = 5;
int y = 7;
System.out.println("x="+x);
System.out.println("y="+y);
x = x + y;
y = x - y;
x = x - y;
System.out.println("x ="+x);
System.out.println("y="+y);
}
}

Q:3)wap to find the greatest among two numbers.


ANS:

public class greatest


{
void great()
{
int a = 39;
int b = 50;
if(a > b);
System.out.println("a is the greatest number");
Else
System.out.println("b is the greatest number");

}
}
Q:4) state the different types of comments used in
java programming and how it should be used.
ANS: The java language supports three types of
comments;
/*text/
The compiler ignores everything from /*to*/.
/**documentation*/
This indicates a documentation comment (doc
comment, for short).The compiler ignores this kind
of comment, just like it ignores comments, just like
it ignores comments that use /*and*/.
//text
The compiler ignores everything from // to the end
of the file
(journals.ecs.soton.ac.uk, 2015).
Q:5)State and explain different types of data types
used in java programming.
ANS: The different primitive data types used in java
programming are listed below:
1. Byte: A byte is one of the basic units of memory
made up of 8 individual bits. Byte data types in
java have the following characteristics:
• Minimum value-128
• Maximum value-127
• Default value: 0
Examples:
Byte x = 56
Byte y = 68
2short: A short is twice the size of a byte, i.e. It is
made up of 16-bits.Its chief characteristics are:
• Minimum value:-32,768
• Maximum value:-32,767
• Default value:-0
Examples:
Short x = 9870
Short y = -635
3. Int: An integer is four times the size of a byte
(i.e.it is made up of 32 bits).
• Minimum value: 2,147,483,
• Maximum value: 2,147,483,647
• Default value: 0
4. long: a long data type is twice the size of an
integer, i.e. it is made up of 64 bits. its chief
characteristics are :
• Minimum value:9,223,372,036,854,775,8
• Maximum
value:9,223,372,036,854,775,807
• Default value: 0
5. float: in programming any decimal or
fractional value is called ‘float’. If there is a
decimal after the number it will be classified as
a float.
6.Double: double is a data type that is twice as
the size of a float i.e., It is made up of 64 bits
IEEE floating points.
7.Char: char data type refers to a single
16bit.unicode character. Unicode is a computer
industry standard for representing text related
data. This includes alphabets, symbols ($,*,#,
@,!,etc)
Examples:
Char name = ‘john’
Char name USA’
8.Boolean: Boolean is the smallest data types in
java. i.e, it is made up of only one bit. Thus, a
Boolean data type can have only two values _0
(or false) and 1(true)
Example: Boolean x = true
Boolean y = false
Q.7) Are the following statements illegal?
Justify.
A) char c = ‘a’;
Ans: The statement is legal as data type char is
in small letter and also the use of single
alphabet ‘a’ enclosed by single cotation.
B)int i = c + 10;
Ans: The statement is isn’t legal because value
of variable c isn’t assigned beforehand and data
type int can’t withhold such unknown variable.
3) String name = “pawan”;
Ans: The statement is legal because the first
letter of data type string; s is in capital letter
and identifier name is alphabet which is valid
and assigned name Pawan is enclosed by
double cotation.
4) String char = “Darwin”
Ans: the statement is invalid because the
identifier name is assigned by reserved
keyword char which can’t be done.
5)string place = ‘kathmandu’
Ans:the statement is illegal because the string
value isn’t enclosed by double cotation which
should be done.
6)Int I = 5;
Ans: The statement is illegal because identifier
never takes numerical value in programming.
7) String five = ‘‘3’’;
Ans: The statement is legal because assigned
value 3 is string value itself and enclosed by
double citation.
8) Char b = ‘45’;
Ans : The statement is illegal because only one
character is taken by data type char and in this
case it has used two character 4 and 5.
9) int num = five;
Ans: The statement is illegal because assigned
value must be numerical value rather than
alphabets and if alphabets are used than its
value must be assigned beforehand, but in this
case it’s not done in that way.
10) int salary = 10,000;
Ans: The statement is illegal because comma
can’t be used in assigned value.
11)String _email= “xyz@gmail.com”;
Ans: The statement isn’t legal because
underscore can’t be used before identifier.

Você também pode gostar