Uploaded by Wafiha.arika

Assignment 1 ITEC 1620

advertisement
Assignment 1: ITEC 1620
Total Marks: 24 PART A
1. What is the value of the following Java expression?
8+3*2%1-4/6*8
a. 32
b. 0
c. 16
d. 8
Marks: 1
ANS: d. 8
2. Write a Java Program that asks for an integer N as input and prints the sum of first N
numbers Marks: 4
DONE
Explanation: Sum of first 6 numbers: 1 + 2 + 3 + 4 + 5 + 6 = 21
3. What is the output of the following Java code fragment (given that other fragments of the
code are correct and so the code will run successfully)?
Sample Input
Samput Output
6
21
10
55
int k = 832;
int m = 832 / 10;
k = k % 10;
k = k % 1000;
k = ++k;
k = k++; System.out.println(m); System.out.println(k);
Marks: 2
ANS: OUTPUT:
System.out.println(m) : 83
System.out.println(k) : 4
4. There are two classes given below. Write down the output of the following when you run
the driver class “Assigntmen1.java”.
Marks: 4
public class Assignment1 {
public static void main(String [] args) {
public class Employee {
private String name;
Employee emp1 = new Employee("Smith", 2600);
Employee emp2 = new Employee();
System.out.println("Salary of " + emp1.getName() +
private double salary;
" is: " + emp1.getSalary() + ".\n"
+ "Salary of " + emp2.getName() + " is: " +
emp2.getSalary());
emp2.updateSalary(500);
emp1 = emp2;
emp1.updateSalary(100);
System.out.println("Salary of " + emp1.getName() + "
is: " + emp1.getSalary());
System.out.println("Salary of " + emp2.getName() + "
is:
" + (int) emp2.getSalary()); }
}
public Employee(String n,
double s) { name=n;
salary=s; }
public Employee() {
name="Alex";
salary=3500; public String
getName(){
return name; public double
getSalary(){
return salary;
}
public void
updateSalary(double
increase){ salary = salary +
increase;
}}
}
}
ANS:
The output of your code is:
Salary of Smith is: 2600.0.
Salary of Alex is: 3500.0
Salary of Alex is: 4100.0
Salary of Alex is: 4100
PART B
5. There are some strings given:
String s1 = "asd", s2 = "Zx", s3 = "ASD", s4 = "efg";
Now write down which of the following statements return positive, negative, or 0.
a) s1.compareTo(s2);
b) s3.compareTo(s2);
c) s3.compareTo(s1.toUpperCase());
d) s1.compareTo(s3.toLowerCase());
ANS:
a) positive
b) negative
c) 0
d) 0
Marks: 2
Answer only Positive, Negative, or 0 for each of the above questions. As an example, for
the following statement: s1.compareTo(s4), the answer will be Negative.
6. What is the output of the following Java code fragment (given that other fragments of the
code are correct and so the code will run successfully)?
int k = 832;
int m = 832 / 10;
k = k % 10;
k = k % 1000;
k = ++k;
k = k++;
k = k - 11; System.out.println(Math.pow(k, 2)); System.out.println(Math.abs(k));
Marks 2
ANS:
System.out.println(Math.pow(k, 2)):49
System.out.println(Math.abs(k)):7
7. Write a complete Java Program that will ask for an integer type input and print whether the
given input value is a leap year or not (Solve it using If-Else / If-Else If statements)
Marks: 5
Sample
Input
Samput
Output
2012
Leap Year
2014
Not a Leap
Year
DONE
8. Write a complete Java Program that will ask for a character as input and print whether the
given character is an uppercase letter or a lowercase letter (Solve it using If-Else / If-Else
If statements)
Marks: 4
Sample
Input
Samput
Output
a
lowercase
D
uppercase
DONE
Download