Survey
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
BIM 101 COMPUTER PROGRAMMING-I LAB WORK
Suppose x = 2 and y = 3. Show the output, if any, of the following code.
What is the output if x = 3 and y = 2?
What is the output if x = 3 and y = 3?
if (x > 2)
if (y > 2) {
int z = x + y;
System.out.println("z is " + z);
}
else
System.out.println("x is " + x);
What is the output of the following code if number is 14, 15, or 30?
if (number % 2 == 0)
System.out.println
(number + " is even");
if (number % 5 == 0)
System.out.println
(number + " is multiple of 5");
Assuming that x is 1, show the result of the following Boolean expressions.
(true) && (3 > 4)
!(x > 0) && (x > 0)
(x > 0) || (x < 0)
(x != 0) || (x == 0)
(x >= 0) || (x < 0)
(x != 1) == !(x == 1)
Suppose, when you run the following program, you enter the input 2 3 6 from
the console. What is the output?
public class Test {
public static void main(String[] args) {
java.util.Scanner input = new Java.util.Scanner(System.in);
double x = input.nextDouble();
double y = input.nextDouble();
double z = input.nextDouble();
System.out.println("(x < y && y < z) is " + (x < y && y < z));
System.out.println("(x < y || y < z) is " + (x < y || y < z));
System.out.println("!(x < y) is " + !(x < y));
System.out.println("(x + y < z) is " + (x + y < z));
System.out.println("(x + y > z) is " + (x + y > z));
}
}
Suppose that s1, s2, and s3 are three strings, given as follows:
String s1 = "Welcome to Java";
String s2 = "Programming is fun";
String s3 = "Welcome to Java";
What are the results of the following expressions?
(a)
(b)
(c)
(d)
(e)
(f)
(g)
(h)
(i)
(j)
(k)
(l)
(m)
(n)
(o)
(p)
(q)
(r)
(s)
(t)
(u)
(v)
s1 == s2
s2 == s3
s1.equals(s2)
s1.equals(s3)
s1.compareTo(s2)
s2.compareTo(s3)
s2.compareTo(s2)
s1.charAt(0)
s1.indexOf('j')
s1.indexOf("to")
s1.lastIndexOf('a')
s1.lastIndexOf("o", 15)
s1.length()
s1.substring(5)
s1.substring(5, 11)
s1.startsWith("Wel")
s1.endsWith("Java")
s1.toLowerCase()
s1.toUpperCase()
s1.concat(s2)
s1.contains(s2)
"\t Wel \t".trim()
Show the output of the following statements
System.out.println("1"
System.out.println('1'
System.out.println("1"
System.out.println("1"
System.out.println('1'
+
+
+
+
+
1);
1);
1 + 1);
(1 + 1));
1 + 1);
What does the following statement do?
for ( ; ; ) {
// Do something
}
Convert the following for loop statement to a while loop and to a do-while
loop:
long sum = 0;
for (int i = 0; i <= 1000; i++)
sum = sum + i;
Identify and fix the errors in the following code:
public class Test {
public void main(String[] args) {
for (int i = 0; i < 10; i++);
sum += i;
if (i < j);
System.out.println(i)
else
System.out.println(j);
while (j < 10);
{
j++;
}
do {
j++;
}
while (j < 10)
}}
What is wrong with the following programs?
public class ShowErrors {
public static void main(String[] args) {
int i = 0;
do {
System.out.println(i + 4);
i++;
}
while (i < 10)
}}
What is wrong with the following programs?
public class ShowErrors {
public static void main(String[] args) {
for (int i = 0; i < 10; i++);
System.out.println(i + 4);
}
}
How many times is the println statement executed?
for (int i = 0; i < 10; i++)
for (int j = 0; j < i; j++)
System.out.println(i * j)
Show the output of the following program
public class Test {
public static void main(String[] args) {
for (int i = 1; i < 5; i++) {
int j = 0;
while (j < i) {
System.out.print(j + " ");
j++;
}}}}
Show the output of the following program
public class Test {
public static void main(String[] args) {
int i = 0;
while (i < 5) {
for (int j = i; j > 1; j--)
System.out.print(j + " ");
System.out.println("****");
i++;
}}}
Show the output of the following program
public class Test {
public static void main(String[] args) {
int i = 5;
while (i >= 1) {
int num = 1;
for (int j = 1; j <= i; j++) {
System.out.print(num + "xxx");
num *= 2;
}
System.out.println();
i--;
}}}
What is the wrong with the following code
class Test {
public static void main(String[] args) {
int x;
String y;
System.out.println("x is " + x);
System.out.println("y is " + y);
}}
Show the output of the following program
public class Test {
public static void main(String[] args) {
int i = 1;
do {
int num = 1;
for (int j = 1; j <= i; j++) {
System.out.print(num + "G");
num += 2;
}
System.out.println();
i++;
} while (i <= 5);
}}
Identify and correct the errors in the following program:
public class Test {
public static method1(int n, m) {
n += m;
method2(3.4);
}
public static int method2(int n) {
if (n > 0) return 1;
else if (n == 0) return 0;
else if (n < 0) return –1;
} }
Identify and correct the errors in the following program:
public class Test {
public static void main(String[] args) {
nPrintln(5, "Welcome to Java!");
}
public static void nPrintln(String message, int n) {
int n = 1;
for (int i = 0; i < n; i++)
System.out.println(message);
}}
What is pass-by-value? Show the result of the following program.
public class Test {
public static void main(String[] args) {
int max = 0;
max(1, 2, max);
System.out.println(max);
}
public static void max(
int value1, int value2, int max) {
if (value1 > value2)
max = value1;
else
max = value2;
}}
Show the result of the following programs.
public class Test {
public static void main(String[] args) {
int i = 0;
while (i <= 4) {
method1(i);
i++;
}
System.out.println("i is " + i);
}
public static void method1(int i) {
do {
if (i % 3 != 0)
System.out.print(i + " ");
i--;
}
while (i >= 1);
System.out.println();
}}
What is the output of the following code?
int x = 30;
int[] numbers = new int[x];
x = 60;
System.out.println("x is " + x);
System.out.println("The size of numbers is " + numbers.length);
Which of the following statements are valid?
int i = new int(30);
double d[] = new double[30];
char[] r = new char(1..30);
int i[] = (3, 4, 3, 2);
float f[] = {2.3, 4.5, 6.6};
char[] c = new char();
Identify and fix the errors in the following code:
public class Test {
public static void main(String[] args) {
double[100] r;
for (int i = 0; i < r.length(); i++);
r(i) = Math.random * 100;
}
}
What is the output of the following code?
public class Test {
public static void main(String[] args) {
int list[] = {1, 2, 3, 4, 5, 6};
for (int i = 1; i < list.length; i++)
list[i] = list[i - 1];
for (int i = 0; i < list.length; i++)
System.out.print(list[i] + " ");
}
}
Show the output of the following two programs:
public class Test {
public static void main(String[] args) {
int number = 0;
int[] numbers = new int[1];
m(number, numbers);
System.out.println("number is " + number
+ " and numbers[0] is " + numbers[0]);
}
public static void m(int x, int[] y) {
x = 3;
y[0] = 3;
}
}
Show the output of the following two programs:
public class Test {
public static void main(String[] args) {
int[] list = {1, 2, 3, 4, 5};
reverse(list);
for (int i = 0; i < list.length; i++)
System.out.print(list[i] + " ");
}
public static void reverse(int[] list) {
int[] newList = new int[list.length];
for (int i = 0; i < list.length; i++)
newList[i] = list[list.length - 1 - i];
list = newList;
}}
Show the output of the following code:
int[][] array = {{1, 2}, {3, 4}, {5, 6}};
for (int i = array.length - 1; i >= 0; i——) {
for (int j = array[i].length - 1; j >= 0; j——)
System.out.print(array[i][j] + " ");
System.out.println();
}
Show the output of the following code:
int[][] array = {{1, 2}, {3, 4}, {5, 6}};
int sum = 0;
for (int i = 0; i < array.length; i++)
sum += array[i][0];
System.out.println(sum);
What is wrong with each of the following programs?
public class ShowErrors {
public static void main(String[] args) {
ShowErrors t = new ShowErrors(5);
}}
What is wrong with each of the following program?
public class ShowErrors {
public static void main(String[] args) {
ShowErrors t = new ShowErrors();
t.x();
}
}
What is wrong with each of the following program?
public class ShowErrors {
public void method1() {
Circle c;
System.out.println("What is radius " + c.getRadius());
c = new Circle();
}
}
What is wrong with the following program?
public class ShowErrors {
public static void main(String[] args) {
C c = new C(5.0);
System.out.println(c.value);
}
}
class C {
int value = 2;
}
What is wrong with the following program?
class Test {
public static void main(String[] args) {
A a = new A();
a.print();
}
}
class A {
String s;
A(String newS) {
s = newS;
}
public void print() {
System.out.print(s);
} }
What is the output of the following code?
public class A {
boolean x;
public static void main(String[] args) {
A a = new A();
System.out.println(a.x);
}
}
Add the static keyword in the place of ? if appropriate.
public class Test {
int count;
public ? void main(String[] args) {
...
}
public ? int getCount() {
return count;
}
public ? int factorial(int n) {
int result = 1;
for (int i = 1; i <= n; i++)
result *= i;
return result;
}
}
Show the output of the following program:
public class Test {
public static void main(String[] args) {
Circle circle1 = new Circle(1);
Circle circle2 = new Circle(2);
swap1(circle1, circle2);
System.out.println("After swap1: circle1 = " +
circle1.radius + " circle2 = " + circle2.radius);
swap2(circle1, circle2);
System.out.println("After swap2: circle1 = " +
circle1.radius + " circle2 = " + circle2.radius);
}
public static void swap1(Circle x, Circle y) {
Circle temp = x;
x = y;
y = temp;
}
public static void swap2(Circle x, Circle y) {
double temp = x.radius;
x.radius = y.radius;
y.radius = temp;
}
}
class Circle {
double radius;
Circle(double newRadius) {
radius = newRadius;
}
}
Show the output of the following code:
public class Test {
public static void main(String[] args) {
int[] a = {1, 2};
swap(a[0], a[1]);
System.out.println("a[0] = " + a[0]
+ " a[1] = " + a[1]);
}
public static void swap(int n1, int n2) {
int temp = n1;
n1 = n2;
n2 = temp;
}
}
Show the output of the following code:
public class Test {
public static void main(String[] args) {
T t = new T();
swap(t);
System.out.println("e1 = " + t.e1
+ " e2 = " + t.e2);
}
public static void swap(T t) {
int temp = t.e1;
t.e1 = t.e2;
t.e2 = temp;
}
}
class T {
int e1 = 1;
int e2 = 2;
}
Show the output of the following code:
public class Test {
public static void main(String[] args) {
int[] a = {1, 2};
swap(a);
System.out.println("a[0] = " + a[0]
+ " a[1] = " + a[1]);
}
public static void swap(int[] a) {
int temp = a[0];
a[0] = a[1];
a[1] = temp;
}}
Show the output of the following code:
public class Test {
public static void main(String[] args) {
T t1 = new T();
T t2 = new T();
System.out.println("t1's i = " +
t1.i + " and j = " + t1.j);
System.out.println("t2's i = " +
t2.i + " and j = " + t2.j);
}}
class T {
static int i = 0;
int j = 0;
T() {
i++;
j = 1;
}}
What is the output of the following program?
public class Test {
private static int i = 0;
private static int j = 0;
public static void main(String[] args) {
int i = 2;
int k = 3;
{
int j = 3;
System.out.println("i + j is " + i + j);
}
k = i + j;
System.out.println("k is " + k);
System.out.println("j is " + j);
}}
What is wrong in the following code?
public class C {
private int p;
public C() {
System.out.println("C's no-arg constructor invoked");
this(0);
}
public C(int p) {
p = p;
}
public void setP(int p) {
p = p;
} }
What is wrong in the following code?
public class Test {
private int id;
public void m1() {
this.id = 45;
}
public void m2() {
Test.id = 45;
}
}