Week 2 In-class Participation
Q1) What is the output?
public class Week2Ex {
public static void main(String[] args) {
int i = 5;
[Link]("%d %d %d %n", i++, i++, i++);
[Link]("%d %d %d", i++, ++i, i++);
Q2) Write a program to accept a decimal number from user and
convert it to binary using a while loop.
Input : Enter a decimal number: 8
Output: The equivalent binary number is : 1000
Q3) The program should accept a string from the user. Now, write a
function to remove all the consecutive duplicate characters.
Example:
Input : “rrrrrr”
Output: “r”
Input: “pqrpqr”
Output:”pqr”
Q4) The program should accept a number of rows (N), columns (M),
and elements for an integer array/list from the user. Write a function
to print the sum of each of the row elements in a single line,
separated by a single space.
Example:
Inputs: Number of rows(N): 4
Number of columns(M): 2
Enter array elements:
12
34
56
78
Output: 3 7 11 15
Q5) Predict the output
public class Week2Ex2 {
public static void main(String[] args) {
int arr[] = { 6, 7, 8, 9, 10 };
mul(arr);
for (int i = 0; i < 5; i++) {
[Link](arr[i] + "\t");
}
}
public static void mul(int[] arr) {
for (int i = 0; i < 5; i++)
arr[i] *= i;
}
}
Q6) Write a program to demonstrate a banking system.
Create two .java files 1) [Link] 2) [Link]
[Link] file should contain:
1) Instance variables account number as an integer, Customer
Name as string, and amount as double.
2) Definition for the Parameterized constructor to initialize the
instance variables.
3) Definition for Instance methods:
• Deposit() with the amount as an argument. This amount
should be added to the original amount.
• Withdraw() with amount as an argument. This amount
should be added to the original amount.
• CheckBalance() to display the current amount of the bank
account when the account number is provided as argument.
• Display() should display customer details, i.e., should display
account number, customer name, and amount.
[Link] file should contain:
• This file should have the main().
• Create one instance for the BankAcc class with the help of the
constructor.
• With the help of this instance call the methods -Deposit(),
Withdraw(), CheckBalance() and Display().
Q7) What is the output
[Link]
public class T {
int a;
int b;
public void set(int a, int b) {
b = a;
this.b = b;
}
void display() {
[Link]("a=" + a + " b=" + b);
}
}
[Link]
public class ThisEx {
public static void main(String[] args) {
T ob = new T();
[Link](10, 20);
[Link]();
}
}