0% found this document useful (0 votes)
12 views4 pages

Java Programming Basics Lab Session

Lab session 2 focuses on the basics of Java programming, including class members, data types, control structures, and arrays. It includes pre-lab exercises to test knowledge on Java concepts and in-lab exercises that require writing Java programs for various tasks. The session concludes with a post-lab exercise to evaluate understanding of method calls in Java.

Uploaded by

beshahashenafe20
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views4 pages

Java Programming Basics Lab Session

Lab session 2 focuses on the basics of Java programming, including class members, data types, control structures, and arrays. It includes pre-lab exercises to test knowledge on Java concepts and in-lab exercises that require writing Java programs for various tasks. The session concludes with a post-lab exercise to evaluate understanding of method calls in Java.

Uploaded by

beshahashenafe20
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Lab session 2: Basics of Java Programming Language

Objective

The objective of lab session 2 is


 To understand members of the class
 To differentiate instance and static member of the class
 To understand the concept of data-type, operator, comment, variable and keywords
 To differentiate primitive and reference data-type
 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() [Link]()
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()

1|Page
B. equal() D. equals()
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. [Link] C. [Link]
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];
[Link](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";
[Link](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

Common questions

Powered by AI

In Java, '+' is used for both numeric addition and string concatenation. '+=', a compound assignment operator, adds and assigns the result to a variable. '&' is a bitwise AND operator applied at the bit level to integers. '||' is a logical OR operator used in conditions to combine boolean expressions. Each operator has specific use cases, and understanding these helps in writing precise and efficient code .

Automatic type conversion in Java, also known as type promotion, occurs when an expression involves multiple numeric data types, promoting them to the largest type to prevent data loss. For example, in mixed-type expressions involving double, float, and int, Java promotes the values to double to ensure precision is maintained. This mechanism avoids data loss and ensures mathematical accuracy but can lead to increased memory usage and performance overhead, requiring careful consideration when designing algorithms .

The main method in Java serves as the entry point for program execution. By convention, it is declared as 'public static void main(String[] args)', allowing the program to be run independently and universally recognized by the Java Virtual Machine. Its static nature allows it to be called without the need for creating an instance of the class, making it a unique and essential component of Java application execution .

Control structures, such as 'if-else', 'switch', and loops ('for', 'while', 'do-while'), are crucial in Java for managing program flow and decision-making processes. These structures allow the execution of code based on conditions, enabling dynamic responses to input or environmental changes. Effective use of control structures enhances flexibility, readability, and efficiency of the code, which is vital for developing robust and responsive Java applications .

Reversing every word in a string can be achieved by splitting the string into an array of words, individually reversing each word using StringBuilder or similar classes, and then concatenating them back into a single string. This operation is relevant for applications like data encoding, enhancing text readability, and formatting user-input data, illustrating practical string manipulation skills that are frequently required in software development .

Primitive data types in Java, such as int, char, and boolean, hold their values directly in memory, whereas reference data types, such as arrays and objects, store the memory address where the data is located. This distinction is crucial because it affects the way variables are assigned and passed to methods. For instance, primitive data type assignments reflect changes within a method only locally, while reference data types reflect changes globally because they refer to the memory address holding the actual data. Understanding this concept is essential for effective memory management and avoiding unintended side-effects in Java programming .

Understanding the hierarchy where 'java.lang' is the superclass for 'String' and 'StringBuffer' is crucial because it elucidates inheritance and polymorphism mechanisms in Java. This knowledge allows developers to utilize shared methods and properties efficiently, ensuring consistent object behavior and enabling flexible design patterns for more modular and maintainable code bases .

Static members belong to the class itself rather than any particular instance, meaning they are shared among all instances, whereas instance members belong to each specific object. Static members are ideal for constants or methods that should be shared across all instances, such as utility functions (e.g., Math methods). Instance members are suited for attributes and methods that require instance-specific data. Understanding and leveraging these differences helps in organizing code logically to optimize performance and maintainability of Java applications .

The 'void' return type in Java indicates that a method does not return any value, differentiating it from methods that return specific data types like int, double, or String. This is used for methods that perform actions but do not need to convey any information back to the caller, such as updating class variables or printing output to the console .

In Java, memory allocation for arrays uses the 'new' operator, which allocates memory in the heap for the specified array. When declaring arrays, one must ensure the array data type is compatible with the elements it will store and that appropriate memory is allocated to prevent java.lang.ArrayIndexOutOfBoundsException errors. Careful memory allocation is crucial for ensuring efficient resource use and preventing excessive memory consumption .

You might also like