├── .idea ├── .gitignore ├── codeStyles │ └── codeStyleConfig.xml ├── dictionaries │ └── divyanshbhardwaj.xml ├── misc.xml ├── modules.xml ├── uiDesigner.xml └── vcs.xml ├── README.MD ├── oops_module2_practice_questions.iml └── src ├── collection ├── question1 │ ├── Solution1.java │ └── output.txt ├── question2 │ ├── Solution2.java │ └── output.txt ├── question3 │ ├── Book.java │ └── Solution3.java ├── question4 │ ├── Solution4.java │ └── Student.java └── question5 │ └── Solution5.java ├── generic ├── question1 │ ├── Name.java │ ├── Solution1.java │ └── Student.java └── question2 │ └── Solution2.java ├── multithreading ├── question1 │ ├── Solution1.java │ ├── Task1.java │ └── Task2.java ├── question2 │ ├── Solution2.java │ ├── Task1.java │ ├── Task2.java │ └── output.txt ├── question3 │ ├── Solution3.java │ └── output.txt ├── question4 │ ├── Solution4.java │ └── output.txt ├── question5 │ └── Solution5.java ├── question6 │ └── Solution6.java ├── question7 │ ├── Solution7.java │ ├── Task1.java │ └── Task2.java └── question8 │ ├── Solution8.java │ ├── Task1.java │ ├── Task2.java │ ├── Task3.java │ ├── Task4.java │ └── output.txt ├── src.iml ├── src └── exceptionHandling │ ├── question1 │ ├── MyFirstException.java │ └── MyMain.java │ ├── question2 │ └── Main.java │ ├── question3 │ └── Main.java │ ├── question4 │ └── Main.java │ └── question5 │ └── Main.java ├── stringhandling ├── question1 │ └── Solution1.java ├── question2 │ └── Solution2.java ├── question3 │ └── Solution3.java ├── question4 │ └── Solution4.java └── question5 │ └── Solution5.java └── wrapper ├── question1 └── Solution1.java ├── question2 └── Solution2.java ├── question3 └── Solution3.java ├── question4 └── Solution4.java ├── question5 └── Solution5.java └── question6 └── Solution6.java /.idea/.gitignore: -------------------------------------------------------------------------------- 1 | # Default ignored files 2 | /workspace.xml -------------------------------------------------------------------------------- /.idea/codeStyles/codeStyleConfig.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | -------------------------------------------------------------------------------- /.idea/dictionaries/divyanshbhardwaj.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | divyanshbhardwaj 5 | 6 | 7 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /.idea/uiDesigner.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /README.MD: -------------------------------------------------------------------------------- 1 | # ObjectOrientedProgramming_Module2_practice_questions 2 | 3 | ### Exception Handling 4 | 1. Define an custom exception (i.e. user defined exception) class called "MyFirstException". This class contains a parameterized 5 | constructor which accept one argument String message. Whenever this exception arise will print "Custom Exception Occurred". 6 | Create another class called MyMain. This class contains main method, to perform sum of two user given values by using Scanner class. 7 | If the sum is less than 150 throw user defined exception or else display sum. 8 | 9 | input 10 | a = 30; 11 | b = 40; 12 | sum = a + b; 13 | as sum is 70 .. then 14 | 15 | output 16 | Custom Exception Occurred 17 | 18 | 2. Write a program to demonstrate exception handling for ArrayIndexOutOfBoundsException, using command line arguments. 19 | 3. Write a program to accept numbers from user and handle all the possible unchecked exception that can occur. 20 | 4. Write a program to print a given string character by character and handle StringIndexOutOfBoundsException. 21 | 5. Write a program to handle ClassCastException, by doing explicit casting of Object to Integer. 22 | 23 | ### String, StringBuilder, StringBuffer 24 | 1. Write a program to concatenate StringBuilder & StringBuffer objects. 25 | 2. Write a program to get a substring of a StringBuffer. 26 | 3. Write a program to display the length and capacity of String, StringBuilder and StringBuffer. 27 | 4. Write a program to check whether two given strings contains same set of characters as well as having same length. 28 | 29 | input 30 | String s1 = "amar"; 31 | String s2= "rama"; 32 | 33 | output : Both contains same characters 34 | 35 | 5. Write a program to lexicographically arrange the given strings "Raman" , "Aman" , "Vikram" , "Shyam" and "Bhuvan". 36 | 37 | ### Wrapper Classes (Integer, Byte, Short, Long, Float, Double, Character, Boolean) 38 | 1. Create objects of all the wrapper classes and print then on console, with using constructor. 39 | 2. Write a program to demonstrate boxing and un-boxing. 40 | 3. Write a program to demonstrate autoboxing and unboxing. 41 | 4. Create an array of 5 integers and print sum and average by creating Integer sum(Integer[] arr) and 42 | Double average(Integer sum, Integer numberOfElements). Do casting as required for getting proper result; 43 | 5. Write a program to print ASCII values of Character objects using wrapper class. 44 | 6. Write a program to print the smaller of the two given Integer, Double and Character Objects. 45 | 46 | ### Multithreading 47 | 1. Write a program to demonstrate multi threading. 48 | 49 | solution [https://github.com/divaibhav/oops_module2_practice_questions/blob/master/src/multithreading/question1/Solution1.java] 50 | 51 | Task1 class [https://github.com/divaibhav/oops_module2_practice_questions/blob/master/src/multithreading/question1/Task1.java] 52 | 53 | Task2 class[https://github.com/divaibhav/oops_module2_practice_questions/blob/master/src/multithreading/question1/Task2.java] 54 | 2. Create two threads t1 & t2, t1 will print all the even numbers from 0 to 100 and t2 will print all the odd numbers from 100 to 0. 55 | Both threads will goto sleep for 1 sec after printing one number. 56 | 57 | solution [https://github.com/divaibhav/oops_module2_practice_questions/blob/master/src/multithreading/question2/Solution2.java] 58 | 59 | Task1 class [https://github.com/divaibhav/oops_module2_practice_questions/blob/master/src/multithreading/question2/Task1.java] 60 | 61 | Task2 class [https://github.com/divaibhav/oops_module2_practice_questions/blob/master/src/multithreading/question2/Task2.java] 62 | 63 | 3. Write a program to print the priority of main thread. 64 | 65 | solution [https://github.com/divaibhav/oops_module2_practice_questions/blob/master/src/multithreading/question3/Solution3.java] 66 | 4. Write a program to set main thread priority to maximum. 67 | 68 | solution [https://github.com/divaibhav/oops_module2_practice_questions/blob/master/src/multithreading/question4/Solution4.java] 69 | 5. What will happen if you accidentally set thread priority to 100?. Write program to demonstrate it. 70 | 6. Write program to demonstrate use of Thread.sleep() method. 71 | 7. Write a program to print 3 states of Thread. 72 | 8. Create 2 threads t1 and t2 by extending Thread class and print hello and world respectively by t1 and t2. Create 2 more threads 73 | t3 & t4 by implementing Runnable interface to print java & programming respectively by t3 & t4. 74 | 75 | solution [https://github.com/divaibhav/oops_module2_practice_questions/blob/master/src/multithreading/question8/Solution8.java] 76 | 77 | Task1 class [https://github.com/divaibhav/oops_module2_practice_questions/blob/master/src/multithreading/question8/Task1.java] 78 | 79 | Task2 class [https://github.com/divaibhav/oops_module2_practice_questions/blob/master/src/multithreading/question8/Task2.java] 80 | 81 | Task3 class [https://github.com/divaibhav/oops_module2_practice_questions/blob/master/src/multithreading/question8/Task3.java] 82 | 83 | Task4 class [https://github.com/divaibhav/oops_module2_practice_questions/blob/master/src/multithreading/question8/Task4.java] 84 | 85 | ### Generics 86 | 1. Create a generic Student class to demonstrate use of generic. Student class contains a field called name, this field will show generic behavior. 87 | It can be of Type String or Name, where name is a class representing name by two fields String firstName and String LastName. 88 | 89 | solution [https://github.com/divaibhav/oops_module2_practice_questions/blob/master/src/generic/question1/Solution1.java] 90 | 91 | Name class [https://github.com/divaibhav/oops_module2_practice_questions/blob/master/src/generic/question1/Name.java] 92 | 93 | Student class [https://github.com/divaibhav/oops_module2_practice_questions/blob/master/src/generic/question1/Student.java] 94 | 95 | 2. Create a generic method sum which accept array of any subtype of Number and return a double value by adding all the elements of the given array. 96 | 97 | solution [https://github.com/divaibhav/oops_module2_practice_questions/blob/master/src/generic/question2/Solution2.java] 98 | 99 | ### Collection (ArrayList & HashSet) 100 | 1. Create a List & Set of 10 integers and demonstrate ordered and unordered collection as well as duplicate allowed and not allowed. 101 | Demonstrate addAll() method also. 102 | 103 | solution [https://github.com/divaibhav/oops_module2_practice_questions/blob/master/src/collection/question1/Solution1.java] 104 | 2. Create a List of 10 Integer objects and try to access 15th index. Properly analysis the output. 105 | 106 | solution [https://github.com/divaibhav/oops_module2_practice_questions/blob/master/src/collection/question2/Solution2.java] 107 | 3. Create a List of 10 Book objects, where Book is a user defined or custom class. Book class contains following field String bookName, 108 | String author, long isbn, & double price. Book class must be properly encapsulated and must implement data hiding. Book class contains parameterized constructors 109 | and no-argument constructor. Book class must override String toString() of Object class to represent Book object as string while printing. 110 | Kindly sort the list of book by price. 111 | 112 | solution [https://github.com/divaibhav/oops_module2_practice_questions/blob/master/src/collection/question3/Solution3.java] 113 | 114 | Book class [https://github.com/divaibhav/oops_module2_practice_questions/blob/master/src/collection/question3/Book.java] 115 | 116 | 4. Create a List of 10 Student objects, where Student is a user defined class. Student class contains three private fields int rollNo, String name, and 117 | double cpi. Student class must override String toString() method of Object class. Kindly sort the student by there name. And also delete the students whose name 118 | starts with "A" & "J". 119 | 120 | solution [https://github.com/divaibhav/oops_module2_practice_questions/blob/master/src/collection/question4/Solution4.java] 121 | 122 | Student class [https://github.com/divaibhav/oops_module2_practice_questions/blob/master/src/collection/question4/Student.java] 123 | 5. Create a list of 10 Integer objects and process the list element by element to count objects containing even value. Print the total number of odd objects after counting even objects. 124 | 125 | solution [https://github.com/divaibhav/oops_module2_practice_questions/blob/master/src/collection/question5/Solution5.java] 126 | -------------------------------------------------------------------------------- /oops_module2_practice_questions.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /src/collection/question1/Solution1.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Created by IntelliJ IDEA. 3 | * User: Vaibhav 4 | * Date: 31-Oct-19 5 | * Time: 3:29 PM 6 | */ 7 | /* 8 | Create a List & Set of 10 integers and demonstrate ordered and unordered collection as well as 9 | duplicate allowed and not allowed. Demonstrate addAll() method also. 10 | 11 | */ 12 | package collection.question1; 13 | 14 | import java.util.ArrayList; 15 | import java.util.HashSet; 16 | import java.util.Iterator; 17 | 18 | public class Solution1 { 19 | public static void main(String[] args) { 20 | //creating a ArrayList of 10 Integers 21 | ArrayList integers = new ArrayList<>(); 22 | // adding elements in ArrayList by using add(Element e) method 23 | // by passing primitive value, which will be converted to object by autoboxing 24 | integers.add(20); 25 | //creating Integer object to represent primitive value 40 by using valueOf() method 26 | //using boxing 27 | Integer obj = Integer.valueOf(40); 28 | //adding obj to list 29 | integers.add(obj); 30 | 31 | // likewise added 8 more elements in list 32 | integers.add(60); 33 | integers.add(70); 34 | integers.add(50); 35 | integers.add(30); 36 | integers.add(250); 37 | integers.add(3); 38 | integers.add(2); 39 | integers.add(10); 40 | //printing the list 41 | System.out.println("printing list"); 42 | System.out.println(integers); 43 | 44 | // or us can use enhanced for loop 45 | System.out.println("printing list using enhanced for loop"); 46 | for (Integer integer : integers) { 47 | // unboxing object to primitive 48 | int no = integer.intValue(); 49 | System.out.println(no); 50 | } 51 | // or with ArrayList you can use normal for loop 52 | System.out.println("printing list using for loop"); 53 | for (int i = 0; i < integers.size(); i++) { 54 | //if you forgot to un-box object to primitive compiler will auto un-box for you 55 | // accessing each element by index using get()method 56 | int no = integers.get(i); 57 | System.out.println(no); 58 | 59 | } 60 | 61 | //creating HashSet to store integers, set is unordered and cannot contain duplicate 62 | HashSet integerHashSet = new HashSet<>(); 63 | 64 | //adding elements 65 | integerHashSet.add(40); 66 | integerHashSet.add(1000); 67 | integerHashSet.add(350); 68 | integerHashSet.add(150); 69 | integerHashSet.add(1050); 70 | integerHashSet.add(3500); 71 | integerHashSet.add(1010); 72 | integerHashSet.add(10010); 73 | integerHashSet.add(3150); 74 | integerHashSet.add(1001); 75 | 76 | //printing the set 77 | System.out.println("printing set"); 78 | System.out.println(integerHashSet); 79 | //printing the set using enhanced for loop 80 | System.out.println("printing list using enhanced for loop"); 81 | for (Integer integer : integerHashSet) { 82 | System.out.println(integer); 83 | } 84 | 85 | // adding set to list by using addAll() method 86 | integers.addAll(integerHashSet); 87 | //printing the list 88 | System.out.println("list with added set"); 89 | System.out.println(integers); 90 | 91 | // adding list to set by using addAll() method this will discard duplicates 92 | 93 | integerHashSet.addAll(integers); 94 | 95 | //printing the set 96 | System.out.println("set with added list"); 97 | System.out.println(integerHashSet); 98 | 99 | //delete 100 | Iterator itr = integerHashSet.iterator(); 101 | System.out.println(itr.next().getClass()); 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | } 112 | } 113 | -------------------------------------------------------------------------------- /src/collection/question1/output.txt: -------------------------------------------------------------------------------- 1 | Output: 2 | printing list 3 | [20, 40, 60, 70, 50, 30, 250, 3, 2, 10] 4 | printing list using enhanced for loop 5 | 20 6 | 40 7 | 60 8 | 70 9 | 50 10 | 30 11 | 250 12 | 3 13 | 2 14 | 10 15 | printing list using for loop 16 | 20 17 | 40 18 | 60 19 | 70 20 | 50 21 | 30 22 | 250 23 | 3 24 | 2 25 | 10 26 | printing set 27 | [1010, 150, 40, 1000, 1001, 1050, 10010, 3500, 350, 3150] 28 | printing list using enhanced for loop 29 | 1010 30 | 150 31 | 40 32 | 1000 33 | 1001 34 | 1050 35 | 10010 36 | 3500 37 | 350 38 | 3150 39 | list with added set 40 | [20, 40, 60, 70, 50, 30, 250, 3, 2, 10, 1010, 150, 40, 1000, 1001, 1050, 10010, 3500, 350, 3150] 41 | set with added list 42 | [2, 3, 70, 40, 1000, 1001, 10, 3500, 3150, 1010, 50, 20, 150, 1050, 10010, 250, 60, 350, 30] 43 | -------------------------------------------------------------------------------- /src/collection/question2/Solution2.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Created by IntelliJ IDEA. 3 | * User: Vaibhav 4 | * Date: 31-Oct-19 5 | * Time: 5:51 PM 6 | */ 7 | /* 8 | Create a List of 10 Integer objects and try to access 15th index. Properly analysis the output. 9 | */ 10 | package collection.question2; 11 | 12 | import java.util.ArrayList; 13 | 14 | public class Solution2 { 15 | public static void main(String[] args) { 16 | //creating a ArrayList of 10 Integers 17 | ArrayList integers = new ArrayList<>(); 18 | // adding elements in ArrayList by using add(Element e) method 19 | // by passing primitive value, which will be converted to object by autoboxing 20 | integers.add(20); 21 | //creating Integer object to represent primitive value 40 by using valueOf() method 22 | //using boxing 23 | Integer obj = Integer.valueOf(40); 24 | //adding obj to list 25 | integers.add(obj); 26 | 27 | // likewise added 8 more elements in list 28 | integers.add(60); 29 | integers.add(70); 30 | integers.add(50); 31 | integers.add(30); 32 | integers.add(250); 33 | integers.add(3); 34 | integers.add(2); 35 | integers.add(10); 36 | //printing the list 37 | System.out.println("printing list"); 38 | System.out.println(integers); 39 | //to get the size of list 40 | int size = integers.size(); 41 | System.out.println("size = " + size); 42 | // try to access the 15th index, will cause "java.lang.IndexOutOfBoundsException". 43 | int no = integers.get(15); 44 | System.out.println(no); 45 | 46 | } 47 | 48 | } 49 | -------------------------------------------------------------------------------- /src/collection/question2/output.txt: -------------------------------------------------------------------------------- 1 | printing list 2 | [20, 40, 60, 70, 50, 30, 250, 3, 2, 10] 3 | size = 10 4 | Exception in thread "main" java.lang.IndexOutOfBoundsException: Index 15 out of bounds for length 10 5 | at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64) 6 | at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70) 7 | at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:248) 8 | at java.base/java.util.Objects.checkIndex(Objects.java:372) 9 | at java.base/java.util.ArrayList.get(ArrayList.java:458) 10 | at collection.question2.Solution2.main(Solution2.java:43) -------------------------------------------------------------------------------- /src/collection/question3/Book.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Created by IntelliJ IDEA. 3 | * User: Vaibhav 4 | * Date: 31-Oct-19 5 | * Time: 5:59 PM 6 | */ 7 | package collection.question3; 8 | /* 9 | custom Book class 10 | for sorting custom class objects your class must implements Comparable interface 11 | or implements Comparator interface 12 | */ 13 | public class Book implements Comparable { 14 | //instance fields 15 | private String bookName; 16 | private String author; 17 | private long isbn; 18 | private double price; 19 | //parameterized constructor 20 | public Book(String bookName, String author, long isbn, double price) { 21 | this.bookName = bookName; 22 | this.author = author; 23 | this.isbn = isbn; 24 | this.price = price; 25 | } 26 | //no-argument constructor 27 | public Book() { 28 | super(); 29 | } 30 | //getter and setter 31 | public String getBookName() { 32 | return bookName; 33 | } 34 | 35 | public String getAuthor() { 36 | return author; 37 | } 38 | 39 | public long getIsbn() { 40 | return isbn; 41 | } 42 | 43 | public double getPrice() { 44 | return price; 45 | } 46 | 47 | //overriding toString() method 48 | 49 | @Override 50 | public String toString() { 51 | return "Book{" + 52 | "bookName='" + bookName + '\'' + 53 | ", author='" + author + '\'' + 54 | ", isbn=" + isbn + 55 | ", price=" + price + 56 | '}'; 57 | } 58 | //method used for sorting 59 | @Override 60 | public int compareTo(Book o) { 61 | //sorting on the basis of price 62 | if(this.price < o.price){ 63 | return -1; 64 | } 65 | else if(this.price > o.price){ 66 | return 1; 67 | } 68 | else { 69 | return 0; 70 | } 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /src/collection/question3/Solution3.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Created by IntelliJ IDEA. 3 | * User: Vaibhav 4 | * Date: 31-Oct-19 5 | * Time: 5:59 PM 6 | */ 7 | /* 8 | Create a List of 10 Book objects, where Book is a user defined or custom class. 9 | Book class contains following field String bookName, String author, long isbn, & double price. 10 | Book class must be properly encapsulated and must implement data hiding. 11 | Book class contains parameterized constructors and no-argument constructor. 12 | Book class must override String toString() of Object class to represent Book object as string 13 | while printing. Kindly sort the list of book by price. 14 | */ 15 | package collection.question3; 16 | 17 | import javax.security.sasl.SaslClient; 18 | import java.util.ArrayList; 19 | import java.util.Collections; 20 | import java.util.Scanner; 21 | 22 | public class Solution3 { 23 | public static void main(String[] args) { 24 | //created list of books 25 | ArrayList bookList = new ArrayList<>(); 26 | // taking user input by using Scanner class 27 | Scanner sc = new Scanner(System.in); 28 | // to add 10 books 29 | for (int i = 0; i < 10; i++) { 30 | //reading input from user 31 | String bookName = sc.nextLine(); 32 | String author = sc.nextLine(); 33 | long isbn = sc.nextLong(); 34 | double price = sc.nextDouble(); 35 | sc.nextLine(); 36 | // creating book object using parameterized constructor 37 | Book book = new Book(bookName,author,isbn,price); 38 | //adding bookmto list 39 | bookList.add(book); 40 | } 41 | //printing bookList using enhanced for loop 42 | System.out.println("books"); 43 | for (Book book : bookList) { 44 | System.out.println(book); 45 | 46 | } 47 | 48 | //sorting books by using Collections.sort() method 49 | Collections.sort(bookList); 50 | 51 | System.out.println("sorted book"); 52 | for (Book book : bookList) { 53 | System.out.println(book); 54 | 55 | } 56 | 57 | 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /src/collection/question4/Solution4.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Created by IntelliJ IDEA. 3 | * User: Vaibhav 4 | * Date: 31-Oct-19 5 | * Time: 10:26 PM 6 | */ 7 | package collection.question4; 8 | 9 | import java.util.ArrayList; 10 | import java.util.Collections; 11 | import java.util.Scanner; 12 | 13 | public class Solution4 { 14 | public static void main(String[] args) { 15 | ArrayList studentList= new ArrayList<>(); 16 | Scanner sc = new Scanner(System.in); 17 | 18 | for (int i = 0; i < 10; i++) { 19 | Student s = new Student(); 20 | s.setName(sc.nextLine()); 21 | s.setRollNo(sc.nextInt()); 22 | s.setCpi(sc.nextDouble()); 23 | sc.nextLine(); 24 | studentList.add(s); 25 | } 26 | System.out.println("printing student list"); 27 | for (Student student : studentList) { 28 | System.out.println(student); 29 | } 30 | 31 | Collections.sort(studentList); 32 | System.out.println("sorted list"); 33 | for (Student student : studentList) { 34 | System.out.println(student); 35 | } 36 | 37 | for (Student student : studentList){ 38 | if(student.getName().startsWith("A") || student.getName().startsWith("J")){ 39 | studentList.remove(student); 40 | } 41 | } 42 | System.out.println("printing student list"); 43 | for (Student student : studentList) { 44 | System.out.println(student); 45 | } 46 | 47 | 48 | 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /src/collection/question4/Student.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Created by IntelliJ IDEA. 3 | * User: Vaibhav 4 | * Date: 31-Oct-19 5 | * Time: 10:27 PM 6 | */ 7 | package collection.question4; 8 | 9 | public class Student implements Comparable{ 10 | private String name; 11 | private int rollNo; 12 | private double cpi; 13 | 14 | public String getName() { 15 | return name; 16 | } 17 | 18 | public void setName(String name) { 19 | this.name = name; 20 | } 21 | 22 | public int getRollNo() { 23 | return rollNo; 24 | } 25 | 26 | public void setRollNo(int rollNo) { 27 | this.rollNo = rollNo; 28 | } 29 | 30 | public double getCpi() { 31 | return cpi; 32 | } 33 | 34 | public void setCpi(double cpi) { 35 | this.cpi = cpi; 36 | } 37 | 38 | @Override 39 | public String toString() { 40 | return "Student{" + 41 | "name='" + name + '\'' + 42 | ", rollNo=" + rollNo + 43 | ", cpi=" + cpi + 44 | '}'; 45 | } 46 | 47 | @Override 48 | public int compareTo(Student o) { 49 | int response = this.name.compareTo(o.name); 50 | return response; 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /src/collection/question5/Solution5.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Created by IntelliJ IDEA. 3 | * User: Vaibhav 4 | * Date: 31-Oct-19 5 | * Time: 10:46 PM 6 | */ 7 | package collection.question5; 8 | 9 | import java.util.ArrayList; 10 | 11 | public class Solution5 { 12 | public static void main(String[] args) { 13 | //creating a ArrayList of 10 Integers 14 | ArrayList integers = new ArrayList<>(); 15 | // adding elements in ArrayList by using add(Element e) method 16 | // by passing primitive value, which will be converted to object by autoboxing 17 | integers.add(20); 18 | //creating Integer object to represent primitive value 40 by using valueOf() method 19 | //using boxing 20 | Integer obj = Integer.valueOf(40); 21 | //adding obj to list 22 | integers.add(obj); 23 | 24 | // likewise added 8 more elements in list 25 | integers.add(60); 26 | integers.add(70); 27 | integers.add(50); 28 | integers.add(30); 29 | integers.add(250); 30 | integers.add(3); 31 | integers.add(2); 32 | integers.add(10); 33 | //printing the list 34 | System.out.println("printing list"); 35 | System.out.println(integers); 36 | int evenCount = 0; 37 | 38 | for (Integer integer : integers) { 39 | if(integer.intValue() % 2 ==0){ 40 | evenCount++; 41 | } 42 | } 43 | int oddCount = integers.size() - evenCount; 44 | System.out.println("odd count = " + oddCount); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/generic/question1/Name.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Created by IntelliJ IDEA. 3 | * User: Vaibhav 4 | * Date: 31-Oct-19 5 | * Time: 11:01 AM 6 | */ 7 | package generic.question1; 8 | 9 | public class Name { 10 | private String firstName; 11 | private String lastName; 12 | 13 | public String getFirstName() { 14 | return firstName; 15 | } 16 | 17 | public void setFirstName(String firstName) { 18 | this.firstName = firstName; 19 | } 20 | 21 | public String getLastName() { 22 | return lastName; 23 | } 24 | 25 | public void setLastName(String lastName) { 26 | this.lastName = lastName; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/generic/question1/Solution1.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Created by IntelliJ IDEA. 3 | * User: Vaibhav 4 | * Date: 31-Oct-19 5 | * Time: 10:59 AM 6 | */ 7 | package generic.question1; 8 | 9 | public class Solution1 { 10 | public static void main(String[] args) { 11 | Student s1 = new Student<>(); 12 | 13 | Student s2 = new Student<>(); 14 | 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /src/generic/question1/Student.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Created by IntelliJ IDEA. 3 | * User: Vaibhav 4 | * Date: 31-Oct-19 5 | * Time: 11:00 AM 6 | */ 7 | package generic.question1; 8 | 9 | public class Student { 10 | private T name; 11 | 12 | public T getName() { 13 | return name; 14 | } 15 | 16 | public void setName(T name) { 17 | this.name = name; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/generic/question2/Solution2.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Created by IntelliJ IDEA. 3 | * User: Vaibhav 4 | * Date: 31-Oct-19 5 | * Time: 10:51 PM 6 | */ 7 | package generic.question2; 8 | 9 | public class Solution2 { 10 | public static void main(String[] args) { 11 | Integer[] arr = {57,61,41,56,47,71,64,43,66,4,54,30}; 12 | Solution2 obj= new Solution2(); 13 | System.out.println("sum = " + obj.sum(arr)); 14 | 15 | } 16 | public double sum(T[] arr){ 17 | double response = 0.0; 18 | for (T t : arr) { 19 | response = response + t.doubleValue(); 20 | } 21 | return response; 22 | 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/multithreading/question1/Solution1.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Created by IntelliJ IDEA. 3 | * User: Vaibhav 4 | * Date: 31-Oct-19 5 | * Time: 11:22 PM 6 | */ 7 | /* 8 | Write a program to demonstrate multi threading. 9 | */ 10 | package multithreading.question1; 11 | 12 | public class Solution1 { 13 | public static void main(String[] args) { 14 | Runnable task1 = new Task1(); 15 | 16 | Runnable task2 = new Task2(); 17 | 18 | Thread t1 = new Thread(task1); 19 | Thread t2 = new Thread(task2); 20 | t1.start(); 21 | t2.start(); 22 | 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/multithreading/question1/Task1.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Created by IntelliJ IDEA. 3 | * User: Vaibhav 4 | * Date: 31-Oct-19 5 | * Time: 11:23 PM 6 | */ 7 | package multithreading.question1; 8 | // creating a task for thread, which will be executed by separate thread 9 | public class Task1 implements Runnable { 10 | 11 | @Override 12 | public void run() { 13 | System.out.println("hello"); 14 | 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/multithreading/question1/Task2.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Created by IntelliJ IDEA. 3 | * User: Vaibhav 4 | * Date: 31-Oct-19 5 | * Time: 11:25 PM 6 | */ 7 | package multithreading.question1; 8 | // creating a task for thread, which will be executed by separate thread 9 | public class Task2 implements Runnable { 10 | 11 | @Override 12 | public void run() { 13 | System.out.println("world"); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/multithreading/question2/Solution2.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Created by IntelliJ IDEA. 3 | * User: Vaibhav 4 | * Date: 31-Oct-19 5 | * Time: 11:29 PM 6 | */ 7 | /* 8 | Create two threads t1 & t2, t1 will print all the even numbers from 0 to 100 and t2 will print all the odd numbers from 100 to 0. Both threads will goto sleep for 1 sec after printing one number. 9 | */ 10 | package multithreading.question2; 11 | 12 | import multithreading.question2.Task1; 13 | import multithreading.question2.Task2; 14 | 15 | public class Solution2 { 16 | public static void main(String[] args) { 17 | Runnable task1 = new Task1(); 18 | 19 | Runnable task2 = new Task2(); 20 | 21 | Thread t1 = new Thread(task1); 22 | Thread t2 = new Thread(task2); 23 | t1.start(); 24 | t2.start(); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/multithreading/question2/Task1.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Created by IntelliJ IDEA. 3 | * User: Vaibhav 4 | * Date: 31-Oct-19 5 | * Time: 11:23 PM 6 | */ 7 | package multithreading.question2; 8 | // creating a task for thread, which will be executed by separate thread 9 | public class Task1 implements Runnable { 10 | 11 | @Override 12 | public void run() { 13 | for (int i = 1; i <= 100 ; i++) { 14 | if(i % 2 == 0){ 15 | System.out.println(i); 16 | try { 17 | Thread.sleep(1000); 18 | } catch (InterruptedException e) { 19 | e.printStackTrace(); 20 | } 21 | } 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/multithreading/question2/Task2.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Created by IntelliJ IDEA. 3 | * User: Vaibhav 4 | * Date: 31-Oct-19 5 | * Time: 11:25 PM 6 | */ 7 | package multithreading.question2; 8 | // creating a task for thread, which will be executed by separate thread 9 | public class Task2 implements Runnable { 10 | 11 | @Override 12 | public void run() { 13 | for (int i = 100; i >= 1 ; i--) { 14 | if(i % 2 != 0){ 15 | System.out.println(i); 16 | try { 17 | Thread.sleep(1000); 18 | } catch (InterruptedException e) { 19 | e.printStackTrace(); 20 | } 21 | } 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/multithreading/question2/output.txt: -------------------------------------------------------------------------------- 1 | output 2 | 3 | 2 4 | 99 5 | 97 6 | 4 7 | 6 8 | 95 9 | 8 10 | 93 11 | 91 12 | 10 13 | 89 14 | 12 15 | 87 16 | 14 17 | 16 18 | 85 19 | 18 20 | 83 21 | 20 22 | 81 23 | 79 24 | 22 25 | 77 26 | 24 27 | -------------------------------------------------------------------------------- /src/multithreading/question3/Solution3.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Created by IntelliJ IDEA. 3 | * User: Vaibhav 4 | * Date: 31-Oct-19 5 | * Time: 11:37 PM 6 | */ 7 | package multithreading.question3; 8 | 9 | public class Solution3 { 10 | public static void main(String[] args) { 11 | Thread currentThread = Thread.currentThread(); 12 | System.out.println("Priority of main Thread = " + currentThread.getPriority()); 13 | 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/multithreading/question3/output.txt: -------------------------------------------------------------------------------- 1 | Priority of main Thread = 5 -------------------------------------------------------------------------------- /src/multithreading/question4/Solution4.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Created by IntelliJ IDEA. 3 | * User: Vaibhav 4 | * Date: 31-Oct-19 5 | * Time: 11:42 PM 6 | */ 7 | package multithreading.question4; 8 | 9 | public class Solution4 { 10 | public static void main(String[] args) { 11 | Thread currentThread = Thread.currentThread(); 12 | currentThread.setPriority(Thread.MAX_PRIORITY); 13 | System.out.println("Priority of main Thread = " + currentThread.getPriority()); 14 | 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/multithreading/question4/output.txt: -------------------------------------------------------------------------------- 1 | Priority of main Thread = 10 -------------------------------------------------------------------------------- /src/multithreading/question5/Solution5.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Created by IntelliJ IDEA. 3 | * User: Vaibhav 4 | * Date: 02-Nov-19 5 | * Time: 1:24 AM 6 | */ 7 | package multithreading.question5; 8 | 9 | public class Solution5 { 10 | public static void main(String[] args) { 11 | Thread t = new Thread(); 12 | //setting priority to 100 13 | t.setPriority(100); //no error at compilation, but generate exception IllegalArgumentException 14 | 15 | } 16 | } 17 | /* 18 | Output: 19 | Exception in thread "main" java.lang.IllegalArgumentException 20 | at java.base/java.lang.Thread.setPriority(Thread.java:1142) 21 | at multithreading.question5.Solution5.main(Solution5.java:13) 22 | 23 | */ -------------------------------------------------------------------------------- /src/multithreading/question6/Solution6.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Created by IntelliJ IDEA. 3 | * User: Vaibhav 4 | * Date: 02-Nov-19 5 | * Time: 1:29 AM 6 | */ 7 | package multithreading.question6; 8 | 9 | public class Solution6 { 10 | public static void main(String[] args) { 11 | //printing number from 1 to 10 with gap of 2 sec using Thread.sleep() method 12 | for (int i = 1; i <= 10 ; i++) { 13 | System.out.println(i); 14 | try { 15 | //sleep() will generate an checked exception InterruptedException 16 | Thread.sleep(2000); 17 | } catch (InterruptedException e) { 18 | e.printStackTrace(); 19 | } 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/multithreading/question7/Solution7.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Created by IntelliJ IDEA. 3 | * User: Vaibhav 4 | * Date: 02-Nov-19 5 | * Time: 1:38 AM 6 | */ 7 | package multithreading.question7; 8 | 9 | public class Solution7 { 10 | public static void main(String[] args) { 11 | Runnable task2 = new Task2(); 12 | Thread t1 = new Task1(); 13 | 14 | Thread t2 = new Thread(task2); 15 | 16 | Thread currentThread = Thread.currentThread(); 17 | t1.start(); 18 | t2.start(); 19 | while(t2.isAlive()){ 20 | 21 | 22 | try { 23 | Thread.sleep(1500); 24 | } catch (InterruptedException e) { 25 | e.printStackTrace(); 26 | } 27 | System.out.println("State of current thread " + currentThread.getState()); 28 | System.out.println("State of t1 thread " + t1.getState()); 29 | System.out.println("State of t2 thread " + t2.getState()); 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/multithreading/question7/Task1.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Created by IntelliJ IDEA. 3 | * User: Vaibhav 4 | * Date: 02-Nov-19 5 | * Time: 1:39 AM 6 | */ 7 | package multithreading.question7; 8 | 9 | public class Task1 extends Thread { 10 | // task is to print hello 10 times with a gap of 5 sec 11 | @Override 12 | public void run() { 13 | for (int i = 0; i < 10; i++) { 14 | try { 15 | Thread.sleep(5000); 16 | System.out.print("hello -> "); 17 | } catch (InterruptedException e) { 18 | e.printStackTrace(); 19 | } 20 | 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/multithreading/question7/Task2.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Created by IntelliJ IDEA. 3 | * User: Vaibhav 4 | * Date: 02-Nov-19 5 | * Time: 1:39 AM 6 | */ 7 | package multithreading.question7; 8 | 9 | import java.util.Scanner; 10 | 11 | public class Task2 implements Runnable { 12 | // task is to print world 10 times with a gap of 2 sec 13 | @Override 14 | public void run() { 15 | Scanner sc =new Scanner(System.in); 16 | for (int i = 0; i < 10 ; i++) { 17 | 18 | System.out.print("world -> "); 19 | sc.nextLine(); 20 | 21 | 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/multithreading/question8/Solution8.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Created by IntelliJ IDEA. 3 | * User: Vaibhav 4 | * Date: 31-Oct-19 5 | * Time: 11:44 PM 6 | */ 7 | /* 8 | Create 2 threads t1 and t2 by extending Thread class and print hello and world respectively by t1 and t2. Create 2 more threads t3 & t4 by implementing Runnable interface to print java & programming respectively by t3 & t4. 9 | */ 10 | package multithreading.question8; 11 | 12 | public class Solution8 { 13 | public static void main(String[] args) { 14 | 15 | Thread t1 = new Task1(); 16 | Thread t2 = new Task2(); 17 | Runnable task3 = new Task3(); 18 | Runnable task4 = new Task4(); 19 | Thread t3 = new Thread(task3); 20 | Thread t4 = new Thread(task4); 21 | t1.start(); 22 | t2.start(); 23 | t3.start(); 24 | t4.start(); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/multithreading/question8/Task1.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Created by IntelliJ IDEA. 3 | * User: Vaibhav 4 | * Date: 31-Oct-19 5 | * Time: 11:45 PM 6 | */ 7 | package multithreading.question8; 8 | 9 | public class Task1 extends Thread { 10 | @Override 11 | public void run() { 12 | System.out.println("hello"); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/multithreading/question8/Task2.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Created by IntelliJ IDEA. 3 | * User: Vaibhav 4 | * Date: 31-Oct-19 5 | * Time: 11:46 PM 6 | */ 7 | package multithreading.question8; 8 | 9 | public class Task2 extends Thread { 10 | @Override 11 | public void run() { 12 | System.out.println("world"); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/multithreading/question8/Task3.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Created by IntelliJ IDEA. 3 | * User: Vaibhav 4 | * Date: 31-Oct-19 5 | * Time: 11:47 PM 6 | */ 7 | package multithreading.question8; 8 | 9 | public class Task3 implements Runnable { 10 | @Override 11 | public void run() { 12 | System.out.println("java"); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/multithreading/question8/Task4.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Created by IntelliJ IDEA. 3 | * User: Vaibhav 4 | * Date: 31-Oct-19 5 | * Time: 11:48 PM 6 | */ 7 | package multithreading.question8; 8 | 9 | public class Task4 implements Runnable { 10 | @Override 11 | public void run() { 12 | System.out.println("Programming"); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/multithreading/question8/output.txt: -------------------------------------------------------------------------------- 1 | Programming 2 | hello 3 | world 4 | java -------------------------------------------------------------------------------- /src/src.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /src/src/exceptionHandling/question1/MyFirstException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Created by IntelliJ IDEA. 3 | * User: divyanshbhardwaj 4 | * Date: 10/24/19 5 | * Time: 3:22 PM 6 | */ 7 | 8 | package src.exceptionHandling.question1; 9 | 10 | public class MyFirstException extends Exception { 11 | 12 | /** 13 | * Parametrized constructor for class 14 | * calls the super class' constructor and passes in the 'message' 15 | */ 16 | public MyFirstException(String message) { 17 | super(message); 18 | } 19 | } -------------------------------------------------------------------------------- /src/src/exceptionHandling/question1/MyMain.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Created by IntelliJ IDEA. 3 | * User: divyanshbhardwaj 4 | * Date: 10/24/19 5 | * Time: 3:02 PM 6 | */ 7 | 8 | package src.exceptionHandling.question1; 9 | 10 | import java.util.Scanner; 11 | 12 | /** 13 | * MyMain class to perform the sum of two user given values 14 | */ 15 | public class MyMain { 16 | public static void main(String[] args) throws MyFirstException { 17 | final Scanner scanner = new Scanner(System.in); 18 | int firstNumber = scanner.nextInt(); 19 | int secondNumber = scanner.nextInt(); 20 | int sumOfTwoUserValues = firstNumber + secondNumber; 21 | if (sumOfTwoUserValues < 150) { 22 | throw new MyFirstException("Custom Exception Occurred"); 23 | } else { 24 | System.out.println("The sum of " + firstNumber + " + " + secondNumber + " = " + sumOfTwoUserValues); 25 | } 26 | } 27 | } -------------------------------------------------------------------------------- /src/src/exceptionHandling/question2/Main.java: -------------------------------------------------------------------------------- 1 | package src.exceptionHandling.question2; 2 | 3 | /** 4 | * This class will input a command line argument from the user and will display array index out of bounds 5 | */ 6 | public class Main { 7 | public static void main(String[] args) { 8 | // check if the user entered any command line argument 9 | if (args.length == 0) { 10 | // the user did not enter any command line arguments 11 | System.out.println("No command line argument given!"); 12 | } else { 13 | int sizeOfArray = Integer.parseInt(args[0]); 14 | int[] integerArray = new int[sizeOfArray]; 15 | // this statement will throw the Exception 16 | integerArray[sizeOfArray + 1] = 4; 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/src/exceptionHandling/question3/Main.java: -------------------------------------------------------------------------------- 1 | package src.exceptionHandling.question3; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | } 6 | } 7 | /* 8 | ArithmeticException: Arithmetic error, such as divide-by-zero. 9 | ArrayIndexOutOfBoundsException: Array index is out-of-bounds. 10 | ArrayStoreException: Assignment to an array element of an incompatible type. 11 | ClassCastException: Invalid cast. 12 | EnumConstantNotPresentException: An attempt is made to use an undefined enumeration value 13 | IllegalArgumentException: Illegal argument used to invoke a method. 14 | IllegalMonitorStateException: Illegal monitor operation, such as waiting on an unlocked thread. 15 | IllegalStateException: Environment or application is in incorrect state. 16 | IllegalThreadStateException: Requested operation not compatible with current thread state. 17 | IndexOutOfBoundsException: Some type of index is out-of-bounds. 18 | NegativeArraySizeException: Array created with a negative size. 19 | NullPointerException: Invalid use of a null reference. 20 | NumberFormatException: Invalid conversion of a string to a numeric format. 21 | SecurityException: Attempt to violate security. 22 | StringIndexOutOfBounds: Attempt to index outside the bounds of a string. 23 | TypeNotPresentException: Type not found. (Added by J2SE 5.) 24 | UnsupportedOperationException: An unsupported operation was encountered. 25 | * */ -------------------------------------------------------------------------------- /src/src/exceptionHandling/question4/Main.java: -------------------------------------------------------------------------------- 1 | package src.exceptionHandling.question4; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Main { 6 | public static void main(String[] args) throws ArrayIndexOutOfBoundsException { 7 | // import the Scanner class and create an object 8 | final Scanner scanner = new Scanner(System.in); 9 | // input a String from the user 10 | String stringValue = scanner.nextLine(); 11 | // close the scanner reference 12 | scanner.close(); 13 | try { 14 | int counter = 0; 15 | char[] characters = stringValue.toCharArray(); 16 | while (counter >= 0) { 17 | System.out.println(characters[counter++]); 18 | } 19 | } catch (ArrayIndexOutOfBoundsException ae) { 20 | System.out.println("Array Indices Went Out Of Bounds!"); 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/src/exceptionHandling/question5/Main.java: -------------------------------------------------------------------------------- 1 | package src.exceptionHandling.question5; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | // create an reference variable of Obejct-type that stores the object of a Double-type 6 | Object object = 3.0; 7 | // try to print the object as an Integer-type value instead of a Double-type value 8 | System.out.println(Integer.valueOf((String) object)); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /src/stringhandling/question1/Solution1.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Created by IntelliJ IDEA. 3 | * User: Vaibhav 4 | * Date: 02-Nov-19 5 | * Time: 2:31 AM 6 | */ 7 | package stringhandling.question1; 8 | 9 | public class Solution1 { 10 | public static void main(String[] args) { 11 | StringBuilder builder = new StringBuilder("hello"); 12 | StringBuffer buffer = new StringBuffer("programming"); 13 | 14 | // there is no concat method either in StringBuilder or in StringBUffer, 15 | // we can use append or String class concat method 16 | 17 | //using String class concat() mehtod 18 | String result = builder.toString().concat(buffer.toString()); 19 | 20 | System.out.println(result); 21 | } 22 | } 23 | /* 24 | Output: 25 | helloprogramming 26 | */ 27 | -------------------------------------------------------------------------------- /src/stringhandling/question2/Solution2.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Created by IntelliJ IDEA. 3 | * User: Vaibhav 4 | * Date: 02-Nov-19 5 | * Time: 2:36 AM 6 | */ 7 | package stringhandling.question2; 8 | 9 | public class Solution2 { 10 | public static void main(String[] args) { 11 | StringBuffer buffer = new StringBuffer("hello programming"); 12 | String result = buffer.substring(5); 13 | System.out.println(result); 14 | } 15 | } 16 | /* 17 | Output: 18 | programming 19 | */ -------------------------------------------------------------------------------- /src/stringhandling/question3/Solution3.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Created by IntelliJ IDEA. 3 | * User: Vaibhav 4 | * Date: 02-Nov-19 5 | * Time: 2:41 AM 6 | */ 7 | package stringhandling.question3; 8 | 9 | public class Solution3 { 10 | public static void main(String[] args) { 11 | String string = new String("Hello"); 12 | StringBuffer buffer = new StringBuffer("Java"); 13 | StringBuilder builder = new StringBuilder("Programming"); 14 | 15 | //for string capacity is always similar to length and there no specific method to get capacity of string 16 | System.out.println("Length and capacity of string is " + string.length()); 17 | System.out.println(); 18 | System.out.println("length of StringBuffer is = " + buffer.length()); 19 | System.out.println("Capacity of StringBuffer is = " + buffer.capacity()); 20 | System.out.println(); 21 | System.out.println("length of StringBuilder is = " + builder.length()); 22 | System.out.println("Capacity of StringBuilder is = " + builder.capacity()); 23 | } 24 | } 25 | /* 26 | Output: 27 | Length and capacity of string is 5 28 | 29 | length of StringBuffer is = 4 30 | Capacity of StringBuffer is = 20 31 | 32 | length of StringBuilder is = 11 33 | Capacity of StringBuilder is = 27 34 | */ 35 | -------------------------------------------------------------------------------- /src/stringhandling/question4/Solution4.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Created by IntelliJ IDEA. 3 | * User: Vaibhav 4 | * Date: 03-Nov-19 5 | * Time: 1:16 PM 6 | */ 7 | package stringhandling.question4; 8 | 9 | public class Solution4 { 10 | public static void main(String[] args) { 11 | String s1 = "amar"; 12 | String s2 = "rama"; 13 | Solution4 obj = new Solution4(); 14 | if(obj.checkStrings(s1,s2)){ 15 | System.out.println("Both contains same characters"); 16 | } 17 | else { 18 | System.out.println("Both contains different characters"); 19 | } 20 | 21 | 22 | 23 | } 24 | 25 | public boolean checkStrings(String s1, String s2) { 26 | boolean response = true; 27 | if(s1.length() == s2.length()){ 28 | for (int i = 0; i < s2.length() ; i++) { 29 | if(s1.contains(""+ s2.charAt(i))){ 30 | continue; 31 | } 32 | else{ 33 | response = false; 34 | break; 35 | } 36 | } 37 | 38 | } 39 | else{ 40 | response = false; 41 | } 42 | 43 | return response; 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/stringhandling/question5/Solution5.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Created by IntelliJ IDEA. 3 | * User: Vaibhav 4 | * Date: 03-Nov-19 5 | * Time: 2:17 PM 6 | */ 7 | package stringhandling.question5; 8 | 9 | import java.util.Arrays; 10 | 11 | public class Solution5 { 12 | public static void main(String[] args) { 13 | String[] strings = {"Raman", "Aman", "Vikram", "Shyam", "Bhuvan"}; 14 | for (int i = 0; i < strings.length - 1; i++) { 15 | for (int j = 0; j < strings.length - 1; j++) { 16 | if (strings[j].compareTo(strings[j + 1]) > 0) { 17 | String temp = strings[j]; 18 | strings[j] = strings[j + 1]; 19 | strings[j + 1] = temp; 20 | } 21 | } 22 | } 23 | System.out.println(Arrays.toString(strings)); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/wrapper/question1/Solution1.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Created by IntelliJ IDEA. 3 | * User: Vaibhav 4 | * Date: 03-Nov-19 5 | * Time: 4:09 PM 6 | */ 7 | package wrapper.question1; 8 | 9 | public class Solution1 { 10 | public static void main(String[] args) { 11 | Integer objInteger = new Integer(10); 12 | Byte objByte = new Byte((byte) 12); 13 | Short objShort = new Short((short) 15); 14 | Long objLong = new Long(123L); 15 | Double objDouble = new Double(12.34); 16 | Float objFloat = new Float(12.3f); 17 | Boolean objBoolean = new Boolean(true); 18 | Character objCharacter = new Character('c'); 19 | 20 | System.out.println(objInteger); 21 | System.out.println(objBoolean); 22 | System.out.println(objByte); 23 | System.out.println(objDouble); 24 | System.out.println(objCharacter); 25 | System.out.println(objFloat); 26 | System.out.println(objLong); 27 | System.out.println(objShort); 28 | 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/wrapper/question2/Solution2.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Created by IntelliJ IDEA. 3 | * User: Vaibhav 4 | * Date: 03-Nov-19 5 | * Time: 4:23 PM 6 | */ 7 | package wrapper.question2; 8 | 9 | public class Solution2 { 10 | public static void main(String[] args) { 11 | //boxing 12 | Integer objInteger = Integer.valueOf(10); 13 | //un-boxing 14 | int a = objInteger.intValue(); 15 | 16 | System.out.println(a); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/wrapper/question3/Solution3.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Created by IntelliJ IDEA. 3 | * User: Vaibhav 4 | * Date: 03-Nov-19 5 | * Time: 4:29 PM 6 | */ 7 | package wrapper.question3; 8 | 9 | public class Solution3 { 10 | public static void main(String[] args) { 11 | //autoboxing 12 | Integer objInteger = 10; 13 | //un-boxing 14 | int a = objInteger; 15 | 16 | System.out.println(a); 17 | 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/wrapper/question4/Solution4.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Created by IntelliJ IDEA. 3 | * User: Vaibhav 4 | * Date: 03-Nov-19 5 | * Time: 4:32 PM 6 | */ 7 | package wrapper.question4; 8 | 9 | public class Solution4 { 10 | public static void main(String[] args) { 11 | Integer[] arr = {10, 20, 30, 40, 50}; 12 | Solution4 obj = new Solution4(); 13 | Integer sum = obj.sum(arr); 14 | Integer numberOfElements = arr.length; 15 | Double ave = obj.average(sum, numberOfElements); 16 | System.out.println("sum = " + sum); 17 | System.out.println("average = " + ave); 18 | 19 | 20 | } 21 | 22 | public Double average(Integer sum, Integer numberOfElements) { 23 | Double response = 0.0; 24 | response = (double) sum / numberOfElements; 25 | return response; 26 | } 27 | 28 | public Integer sum(Integer[] arr) { 29 | Integer response = 0; 30 | for (Integer integer : arr) { 31 | response += integer; 32 | } 33 | return response; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/wrapper/question5/Solution5.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Created by IntelliJ IDEA. 3 | * User: Vaibhav 4 | * Date: 03-Nov-19 5 | * Time: 4:43 PM 6 | */ 7 | package wrapper.question5; 8 | 9 | public class Solution5 { 10 | public static void main(String[] args) { 11 | Character obj = Character.valueOf('A'); 12 | System.out.println((int) obj); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/wrapper/question6/Solution6.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Created by IntelliJ IDEA. 3 | * User: Vaibhav 4 | * Date: 03-Nov-19 5 | * Time: 4:53 PM 6 | */ 7 | package wrapper.question6; 8 | 9 | public class Solution6 { 10 | public static void main(String[] args) { 11 | Integer objInteger1 = Integer.valueOf(100); 12 | Integer objInteger2 = Integer.valueOf(200); 13 | if (Integer.compare(objInteger1, objInteger2) == -1) { 14 | System.out.println(objInteger1); 15 | } else if (Integer.compare(objInteger1, objInteger2) == 1) { 16 | System.out.println(objInteger2); 17 | } else { 18 | System.out.println("equal"); 19 | } 20 | Double objDouble1 = Double.valueOf(12.34); 21 | Double objDouble2 = Double.valueOf(123.43); 22 | if (Double.compare(objDouble1, objDouble2) == -1) { 23 | System.out.println(objDouble1); 24 | } else if (Double.compare(objDouble1, objDouble2) == 1) { 25 | System.out.println(objDouble2); 26 | } else { 27 | System.out.println("equal"); 28 | } 29 | } 30 | } 31 | --------------------------------------------------------------------------------