You are on page 1of 11

Solution

CSE310T

MTE

Ans1: a) The wrapper classes in the Java API serve two primary purposes: a. To provide a mechanism to wrap primitive values in an object so that the primitives can be included in activities reserved for objects, like as being added to Collections, or returned from a method with an object return value. b. To provide an assortment of utility functions for primitives. Most of these functions are related to various conversions: converting primitives to and from String objects, and converting primitives and String objects to and from different bases (or radix), such as binary, octal, and hexadecimal. Primitive datatype-->Wrapper Class-->Constructor arguments

boolean--> Boolean--> boolean or String byte--> Byte--> byte or String char--> Character--> char short--> Short--> short or String int-->Integer--> int or String long--> Long--> long or String float-->Float--> float double or String double-->Double--> double or String

All the wrapper classes are declared final. That means you cannot derive a subclass from any of them. b) Because of this inconsistencies and ambiguities, Multiple inheritance is not permitted in java. Suppose consider a method funX() which is in class Z. Suppose a programmer ABC inherited the class Z to class X and overrided the funX().So this class will have the new implementation of funX(). Suppose a programmer DEF inherited the class Z to class Y and overrided the

funX().So this class will have the new implementation of funX(). If Multiple Inheritance is permitted in java, then if the new programmer inherited both the classes and he didn't done any overriding of method funX() then if he calls the funX() ,the JVM will not know which method to call i.e., either the method in class X or method in class Y. c) final class cannot be inherited d) Thread is a class runnable is an interface 1)If you want to extend the Thread class then it will make your class unable to extend other classes as java is having single inheritance feature whereas If you implement runnable interface, you can gain better object-oriented design and consistency and also avoid the single inheritance problems. 2)Extending the thread will give you simple code structure in comparison to Runnable Interface. 3)Using Runnable Interface, you can run the class several times whereas Thread have the start() method that can be called only once. e) java.lang it has a Runnable interface String, StringBuffer, System, Object, Math, Thread, Throwable, and all the wrapper classes

Ans2: a) 5 15 48 1 9.0 7.5 -4.5 62 62 256 b) class APGP extends Thread { int p1,p2,ap,gp; public APGP() { } public APGP(int p1,int p2,String tname) { super(tname); this.p1 = p1; this.p2 = p2; ap = p1; gp = p1; } public void run() { if(Thread.currentThread().getName().equals("AP")) { System.out.print("AP:"); for(int i=1;i<=10;i++) {

System.out.print(" "+ap); ap+=p2; } System.out.println(); } else if(Thread.currentThread().getName().equals("GP")) { System.out.print("GP:"); for(int i=1;i<=10;i++) { System.out.print(" "+gp); gp*=p2; } System.out.println(); } } } class APGPThreads { public static void main(String[] args) { int a = Integer.parseInt(args[0]); int b = Integer.parseInt(args[1]); APGP ap = new APGP(a,b,"AP"); APGP gp = new APGP(a,b,"GP"); ap.start(); gp.start(); } }

Ans3: String buffer methods: 1. length( ) The current length of a StringBuffer can be found via the length( ) method, while the total allocated capacity can be found through the capacity( ) method. They have the following general forms: int length( ) StringBuffer sb = new StringBuffer("Hello"); System.out.println("length = " + sb.length());

2. charAt( ) and setCharAt( ) The value of a single character can be obtained from a StringBuffer via the charAt( ) method. You can set the value of a character within a StringBuffer using setCharAt( ). Their general forms are shown here: char charAt(int where) void setCharAt(int where, char ch) For charAt( ), where specifies the index of the character being obtained. For setCharAt( ), where specifies the index of the character being set, and ch specifies the new value of that character 3. getChars( ) To copy a substring of a StringBuffer into an array, use the getChars( ) method. It has this general form: void getChars(int sourceStart, int sourceEnd, char target[ ], int targetStart) Here, sourceStart specifies the index of the beginning of the substring, and sourceEnd specifies an index that is one past the end of the desired substring 4. append( ) The append( ) method concatenates the string representation of any other type of data to the end of the invoking StringBuffer object. It has several overloaded versions.

StringBuffer append(String str) 5. insert( ) The insert( ) method inserts one string into another. StringBuffer insert(int index, String str) 6. reverse( ) You can reverse the characters within a StringBuffer object using reverse( ), shown here: StringBuffer reverse( ) 7. delete( ) and deleteCharAt( ) You can delete characters within a StringBuffer by using the methods delete( ) and deleteCharAt( ). StringBuffer delete(int startIndex, int endIndex) StringBuffer deleteCharAt(int loc) 8. replace( ) You can replace one set of characters with another set inside a StringBuffer object by calling replace( ). StringBuffer replace(int startIndex, int endIndex, String str) The substring being replaced is specified by the indexes startIndex and endIndex. Thus, the substring at startIndex through endIndex1 is replaced. The replacement string is passed in str. 9. substring( ) You can obtain a portion of a StringBuffer by calling substring( ). It has the following two forms: String substring(int startIndex) String substring(int startIndex, int endIndex) The first form returns the substring that starts at startIndex and runs to the end of the invoking StringBuffer object. The second form returns the substring that starts at startIndex and runs through endIndex1

Ans4: a) Arrays are passed by reference means when an array is passed as an argument its memory address location is actually passed. Example Public static void hello(int [] i) { i[0]=4; i[1]=9; i[2]=8; }

Example: import java.io.*; public class hello { public static void main (String [ ] args) { int i; int sum=0; for ( i = 0; i < 10; i++ ) number[ i ] = Console.readInt("Enter number: " ); int sum = find_sum(number); System.out.println("The sum is" +sum + "."); } public static int find_sum(int [ ] value) { int i, total = 0; for(i=0; i<10; i++) { total = total + value[ i ];

} return (total); } Arrays can be returned from a method in java as well. The following code shows how a java method returns an array. public class Test { private static int[] arr; public static void main(String[] args) { arr = getArray(); for (int i = 0; i< arr.length; i++) { System.out.println(i); } } public static int[] getArray() { int[] array={1,2,3,4,5,6,7}; return array; } }

Ans4: b) Declaring an array is stating its dimensions, also creating a variable that can hold a reference to one. The array object itself is allocated when the new keyword is used because array is a reference type. If you skip new then array variable itself contains null (which differs from an empty array having 0 elements). if you initialize an array using braces , you put values directly into the elements and skip them receiving the default value. So initialization will occur explicitly if you provide the values otherwise implicitly if they are allowed to default. Sometimes the term "defining" an array is used to mean = declaration + allocation + initialization Array index Each item in an array is called an element, and each element is accessed by its numerical index. Array indexes always begin with zero (0). It specifies the first element of an array where the last index of an array is (n-1) where n specifies the total number of elements in array. Eg. Int [] t={4,5,6}; Here t is an array with size n=3; And t[0]=4 where [0] specifies the index. Bound of an array is the maximum defined size of array. out of bounds exception is occurred when you try to access an array with index that exceeded its length. maximum index of a java array is (length -1) for example: String [] str = new String[10]; str[10] // the code above will produce an out of bounds exception, because the it bigger than length -1, which is 10 - 1 = 9.

Ans5: interface A{ int x=10; void m1(); void m2(); } interface B{ int x=20; void m3(); void m4(); } interface C{ int x=30; void m5(); void m6(); } interface D extends A,B,C{ int x=30; void m7(); } class E{ void m8(){ System.out.println("m8 method of class E "); } } class F extends E implements D{ public void m1(){ System.out.println("m1 method of interface A "+x); } public void m2(){ System.out.println("m2 method of interface A "+x); } public void m3(){ System.out.println("m3 method of interface B "+x); } public void m4(){ System.out.println("m4 method of interface B "+x); } public void m5(){

System.out.println("m5 method of interface C "+x); } public void m6(){ System.out.println("m6 method of interface C "+x); } public void m7(){ System.out.println("m7 method of interface D "+x); } public static void main(String ar[]){ F ob=new F(); ob.m1(); ob.m2(); ob.m3(); ob.m4(); ob.m5(); ob.m6(); ob.m7(); ob.m8(); } } Output: m1 method of interface A 30 m2 method of interface A 30 m3 method of interface B 30 m4 method of interface B 30 m5 method of interface C 30 m6 method of interface C 30 m7 method of interface D 30 m8 method of class E

Interface cannot have private members. So, we cannot declare x as private.

You might also like