You are on page 1of 4

Lab session 2: Basics of Java Programming Language

Objective

The objective of lab session 2 is


 To understand members of the class
 To understand the concept of data-type, operator, comment, variable and keywords
 To solve problems using java control structures
 To understand concepts of Array and String

Pre-lab Exercise

1. Which method can be defined only once in a program?


A. main method C. finalize method
B. static method D. private method
2. What is the range of byte data type in Java?
A. -128 to 127 C. -32768 to 32767
B. -2147483648 to 2147483647 D. None of the mentioned
3. What is the return type of a method that does not return any value?
A. int C. float
B. void D. double
4. Which of the following can be operands of arithmetic operators?
A. Numeric C. Boolean
B. Characters D. Both Numeric & Characters
5. Which of the following statements are incorrect?
A. Static methods can call other static methods only
B. Static methods must only access static data
C. Static methods can not refer to this or super in any way
D. When object of class is declared, each object contains its own copy of static
variables
6. Which of these method of String class is used to obtain character at specified index?
A. char() C.Charat()
B. charat() D. charAt()
7. Which of these method of String class can be used to test to strings for equality?
A. isequal() C. isequals()
B. equal() D. equals()

1|Page
8. Which of these operators is used to allocate memory to array variable in Java?
A. malloc C. alloc
B. new D. new malloc
9. Which of these is an incorrect array declaration?
A. int arr[] = new int[5] C. int [] arr = new int[5]
B. int arr[] = new int[5] D. int arr[] = int [5] new
10. Which of these is necessary condition for automatic type conversion in Java?
A. The destination type is smaller than source type
B. The destination type is larger than source type
C. The destination type can be larger or smaller than source typ
D. None of the mentioned
11. If an expression contains double, int, float, long, then the whole expression will be
promoted into which of these data types?
A. long C. int
B. double D. float
12. Which of these class is superclass of String and StringBuffer class?
A. java.util C. java.lang
B. ArrayList D. None of the mentioned
13. Which of these operators can be used to concatenate two or more String objects?
A. + B. += C. & D. ||

14. What will be the output of the following Java code?


class evaluate
{
public static void main(String args[])
{
int arr[] = new int[] {0 , 1, 2, 3, 4, 5, 6, 7, 8, 9};
int n = 6;
n = arr[arr[n] / 2];
System.out.println(arr[n] / 2);
}
}

15. What will be the output of the following Java program?


class string_demo
{
public static void main(String args[])
{
String obj = "I" + "like" + "Java";
System.out.println(obj);
}
}

2|Page
In-lab Exercise

16. Write a Java program to reverse an array of integer values.


17. Write a Java program to find the duplicate values of an array of integer values.
18. Write a Java program to find the second smallest element in an array
19. Write a Java program to find the second largest element in an array.
20. Write a Java program to add two matrices of the same size
21. Write a Java program to find the number of even and odd integers in a given array of
integers.
22. Given two sorted arrays A and B of size p and q, write a Java program to merge
elements of A with B by maintaining the sorted order i.e. fill A with first p smallest elements
and fill B with remaining elements.
Input :
int[] A = { 1, 5, 6, 7, 8, 10 }
int[] B = { 2, 4, 9 }
Output:
Sorted Arrays:
A: [1, 2, 4, 5, 6, 7]
B: [8, 9, 10]
23. Write a Java program to concatenate a given string to the end of another string. Go to
the editor
Sample Output:
String 1: PHP Exercises and
String 2: Python Exercises
The concatenated string: PHP Exercises and Python Exercises
24. Write a Java program to test if a given string contains the specified sequence of char
values.
Sample Output:
Original String: PHP Exercises and Python Exercises
Specified sequence of char values: and
true
25. Write a Java program to reverse every word in a string using methods.
Sample Output:
The given string is: This is a test string
The string reversed word by word is:
sihT si a tset gnirts

3|Page
26. Write a Java program to count and print all the duplicates in the input string.
Sample Output:
The given string is: w3resource
The duplicate characters and counts are:
e appears 2 times
r appears 2 times

Post-lab Exercise

27. In the following Java code, which call to sum() method is appropriate?

class Output
{

public static int sum(int ...x)


{
return;
}
static void main(String args[])
{
sum(10);
sum(10,20);
sum(10,20,30);
sum(10,20,30,40);
}
}

4|Page

You might also like