Enable contrast version

Tutor profile: Pooja B.

Inactive
Pooja B.
Tutor since 2015 with mastery over subject
Tutor Satisfaction Guarantee

Questions

Subject: Java Programming

TutorMe
Question:

What is the use of keyword final in Java?

Inactive
Pooja B.
Answer:

The final keyword is a modifier used to provide a restriction in Java. As the name suggests, it marks the entity as 'final', that is, the entity cannot further be changed. It is applicable in three different contexts as given below: i. A variable ii. A method iii. A class Let's learn the use of final in these contexts. A variable: The value of a variable declared as final cannot be changed. Example 1, final int TOTAL = 50; Here, the value of the final variable TOTAL cannot be changed. In other words, it behaves like a constant. In practice, a final variable name is declared in all uppercase letters. Example 2, public class demo{ public static void main(String args[]) { final int i = 100; i = 30; // Error because i is final. } } Important points for initialization of a final variable: a. It can only be initialized once. b. If a final variable is not initialized, the compiler throws an error. A final variable that is not initialized during declaration is called a blank final variable. c. A blank final variable can be initialized inside a constructor. d. A blank final static variable can be initialized inside the static block. e.If a final variable is referencing an object, the internal state of the object pointed to by the reference final variable can be changed. A method: A final method cannot be overridden by a subclass. It is used when the class inheritance is needed but a method has to be prohibited from overriding. A method can be made final by writing the keyword final before the method definition. For example, class demo{ final void display(){ System.out.println("Final Class Method"); } } class demo2 extends demo{ void display(){ System.out.println("demo2 Class Method"); } public static void main(String args[]){ demo2 obj= new demo2(); obj.display(); } } Here, the program will throw compile time error as the final method display() is being overridden in the class demo2. Note: A constructor cannot be declared as final. A class: A class that is marked as final cannot be inherited. The wrapper classes like String, Int, etc. in Java are all final classes as they cannot be further inherited. For example, final class demo { private int a; } class demo2 extends demo { } Here, the program will throw compile time error as the final class demo is being inherited by the class demo2.

Subject: Set Theory

TutorMe
Question:

How can a set be represented?

Inactive
Pooja B.
Answer:

A set can be represented in two forms: 1. Roster Form 2. Set Builder Form Roster Form In this form, all the elements of the set are written as a comma-separated list within a pair of curly braces. For example, set A = {1, 2, 3, 4} Here, set A contains 4 elements, that are 1, 2, 3, and 4. This form is used to represent a finite set. That is, the number of elements in the set is finite. Set Builder Form In this form, the elements of a set are defined by using a variable and the property (or properties) which is satisfied by the elements of the set. Also, it is written within a pair of curly braces. For example, set B = {x | x is an integer, x > 12} Here, the set contains all the integers that are greater than 12. This form can be used to represent both, a finite set or an infinite set.

Subject: C Programming

TutorMe
Question:

Explain the concept of Dynamic Memory Allocation in C Programming in detail.

Inactive
Pooja B.
Answer:

Static Memory Allocation: It means that the amount of memory to be allocated for a variable is decided at compile time. When a variable is created using a declaration statement, its memory is allocated statically. For example, int var; Here, the integer variable, var, will be allocated memory statically. That is, the amount of memory to be allocated is fixed and decided at compile time. When the compiler runs through declaration statements, it decides the amount of memory that is to be allocated for the given variable. Dynamic Memory Allocation: It means that the amount of memory to be allocated for a variable is decided at run time (dynamically). In C, the dynamic memory allocation can be implemented using pointer variables. Given below are the four inbuilt functions used in dynamic memory allocation: i. malloc() ii. calloc() iii. realloc() iv. free() These functions are defined in the header file <stdlib.h>. A detailed explanation of these functions is given below: malloc(): It is used to allocate a single block of requested memory. In practice, malloc() is used to allocate the memory to structure type data. Syntax: void *malloc(size_t sz) Here, sz is the requested size in bytes, and the function malloc() returns the address of the memory block allocated. The return type of the function is a generic pointer, that is, the void type pointer. On failure, it returns a NULL value. For example, (int *)malloc(sizeof(int)) Here, the function call will allocate a memory equal to the size of integer type data and will return the address of the same. As the function malloc() returns a void type pointer, it is then typed cast to the required data type, that is, integer type pointer in this example. Note: The allocated memory is initialized with garbage values. calloc(): It is used to allocate contiguous memory space for array type data. Syntax: void *calloc(size_t number_of_elements, size_t sz) Here, number_of_elements is the number of elements of the array for which the memory is to be allocated, and sz is the requested size in bytes for each element of the array. It returns the address of the allocated memory. The return type of the function is the generic pointer, that is, the void type pointer. On failure, it returns a NULL value. For example, (int *)calloc(5, sizeof(int)) Here, the function call will allocate a memory equal to 5 elements of the size of integer type data and will return the address of the same. As calloc() returns a void type pointer, it is then typed cast to the required data type, that is, integer type pointer in this example. Note: The allocated memory is initialized with 0. realloc(): It is used to increase or decrease the size of memory allocated. Syntax: void *realloc(void *ptr, size_t sz) Here, ptr is the pointer containing the address of the block of memory to be resized, sz is the requested new size in bytes, and the function realloc() returns the address of the memory block allocated. The return type of the function is the generic pointer, that is, the void type pointer. On failure, it returns a NULL value. For example, Suppose, a memory block for ‘5 elements of an integer type array’ has been allocated using calloc() function as given here: int *ptr; ptr = (int *)calloc(5, sizeof(int)); Now, if the user wants to increase the size to 7 elements, realloc() function can be used as under: ptr = (int *)realloc(ptr, 7*sizeof(int)); The realloc() function increases the allocated memory size of the pointer ptr to ‘7 elements of an integer type array’ without losing the values in the first 5 elements. Note: The newly allocated memory is uninitialized. free(): This function is used to free the memory allocated dynamically. Using this function makes the memory available when not needed. Syntax: void free(void *pointername); Here, pointername is the pointer whose memory is to be freed. For example, free(ptr); This statement will free the memory allocated to the pointer ptr. The following program shows the behavior of all the functions of dynamic memory allocation. /* This program reads an integer number and prints the first n multiples of the given number. Then the user is asked to have more multiples. Based on the user's input, more multiples of the same number are printed */ #include<stdio.h> #include<stdlib.h> int main() { int i, n, m, *ptr1, *ptr2; //Use of malloc ptr1 = (int *)malloc(sizeof(int)); printf("Enter an integer value: "); scanf("%d", ptr1); printf("You entered: %d", *ptr1); //Use of calloc printf("\n\nHow many multiples of %d do you want? ", *ptr1); scanf("%d", &n); ptr2 = (int *)calloc(n, sizeof(int)); for (i=0; i<n; ++i) { ptr2[i] = *ptr1*(i+1); } printf("First %d multiples of %d are: ", n, *ptr1); for(i=0; i<n; ++i) { printf("%3d", ptr2[i]); } //Use of realloc printf("\n\nHow many more multiples do you want? "); scanf("%d", &m); ptr2 = (int *)realloc(ptr2, (n+m)*sizeof(int)); /*Assigning the values to only extended memory as the previous values will be retained*/ for (i=n; i<n+m; ++i) { ptr2[i] = *ptr1*(i+1); } printf("First %d multiples of %d are: ", n+m, *ptr1); for(i=0; i<n+m; ++i) { printf("%3d", ptr2[i]); } free(ptr1); free(ptr2); return 0; } Output: Enter an integer value: 5 You entered: 5 How many multiples of 5 do you want? 6 First 6 multiples of 5 are: 5 10 15 20 25 30 How many more multiples do you want? 4 First 10 multiples of 5 are: 5 10 15 20 25 30 35 40 45 50

Contact tutor

Send a message explaining your
needs and POOJA will reply soon.
Contact POOJA

Request lesson

Ready now? Request a lesson.
Start Lesson

FAQs

What is a lesson?
A lesson is virtual lesson space on our platform where you and a tutor can communicate. You'll have the option to communicate using video/audio as well as text chat. You can also upload documents, edit papers in real time and use our cutting-edge virtual whiteboard.
How do I begin a lesson?
If the tutor is currently online, you can click the "Start Lesson" button above. If they are offline, you can always send them a message to schedule a lesson.
Who are TutorMe tutors?
Many of our tutors are current college students or recent graduates of top-tier universities like MIT, Harvard and USC. TutorMe has thousands of top-quality tutors available to work with you.
BEST IN CLASS SINCE 2015
TutorMe homepage
Made in California by Zovio
© 2013 - 2021 TutorMe, LLC
High Contrast Mode
On
Off