Python Developer MCQ Test

Instructions

A code snippet is provided, followed by 5 MCQs and 1 coding question. You have to understand the given code, answer the MCQs and write your own code for the given problem.

Code

import os
import time
import threading
import logging
import random
import time
import threading
import subprocess
from ppadb.client import Client as AdbClient
# logging.basicConfig(filename="app.log", level=logging.DEBUG)
import Google_play_signin
from Install_Activity_new import install
from app_activity_new import IN_APP
# from utilityfcn import utility
import xml.etree.ElementTree as ET
import utilityfcn

logger = logging.getLogger()
logger.setLevel(logging.DEBUG)

# Create file handlers for two log files
app_handler = logging.FileHandler('erro.log')
app_handler.setLevel(logging.INFO)

activity_handler = logging.FileHandler('activityinfo.log')
activity_handler.setLevel(logging.DEBUG)

# Create formatter
formatter = logging.Formatter('%(message)s')
mformatter = logging.Formatter('%(message)s')
app_handler.setFormatter(formatter)
activity_handler.setFormatter(mformatter)

# Add handlers to logger
logger.addHandler(app_handler)
logger.addHandler(activity_handler)
#
################ function that contain emulator creation, app opening, closing emulator #####################

def start_end(starting_emulator,ending_emulator,device, client, cnt):

launching=install() ## Creating object for install class
inapp=IN_APP()
#print(device)

print(f"Started emulator -->{starting_emulator}")
logger.info(f"VM {starting_emulator} starts")
packinstallflag, siginflag = launching.checkPackage_signin(client,packagename, starting_emulator, cnt)

# print(packinstallflag, siginflag)
if siginflag==-1: # if not signed in
Google_play_signin.signin(starting_emulator)
logger.info(f'VM {starting_emulator} GOING FOR SIGNIN')
time.sleep(10)
#update the signin flag
packinstallflag ,siginflag = launching.checkPackage_signin(client, packagename, starting_emulator, cnt)

if siginflag==10: #if signed in
if not packinstallflag:
try:
launching.browser(affilate_link,starting_emulator,device)
launching.play_store_install(min_install_time,max_install_time,device)

# while packinstallflag !=True or checkinstall<10:
time.sleep(60)
packinstallflag, siginflag = launching.checkPackage_signin(client, packagename,starting_emulator, cnt)

checkingpack =0

while checkingpack < 10:
if packinstallflag:
break
else:
time.sleep(20)
packinstallflag, siginflag = launching.checkPackage_signin(client, packagename,
starting_emulator, cnt)
checkingpack += 1

packinstallflag, siginflag = launching.checkPackage_signin(client, packagename,starting_emulator, cnt)
if not packinstallflag:
os.system(f'ldconsole quit --index {device}')

except Exception as e:
logging.error("An error occurred: {}".format(e))
logger.info(f'VM {starting_emulator} Failed to package install')

logger.debug(f'{starting_emulator}')

if packinstallflag:
if starting_emulator in random_number_open_app:
try:
randomtimeslp = random.randint(30, 60)
time.sleep(randomtimeslp)
inapp.open_app(device, packagename)
time.sleep(60)
file1 = open("jiocinemainstall.txt", "a+")
file1.write('\n')
file1.write(str(device))
file1.write('\n')
except Exception as e:
logging.error("An error occurred: {}".format(e))
logger.debug(f'{starting_emulator}')

else:
file1 = open("myfile.txt", "a+")
file1.write('\n')
file1.write(str(device))
file1.write('\n')
logging.info(f"skipped opening of app in the emulator number -->{starting_emulator}")
#### Launching the Devices before performing actions on it ####################
def run_ldplayer(emulator_name, ending_emulator, device, client, cnt):
os.system(f"ldconsole launch --index {emulator_name}")
os.system(f"ldconsole reboot --index {emulator_name}")
time.sleep(160)

if utilityfcn.utility.is_adb_running(emulator_name)==False: #check if adb is connected or not
os.system(f"ldconsole reboot --index {emulator_name}")
time.sleep(60)
if utilityfcn.utility.is_adb_running(emulator_name):
print(f"ADB is running after restart for {emulator_name}")
else:
print(f"ADB is not running even after restart for {emulator_name}")
#os.system(("ldconsole sortwnd"))

def thread(flag,total_vm, client):

thr = []
cnt=0
for i in range(flag, flag + no_of_vm):
t = threading.Thread(target=run_ldplayer,args=(i, i+1,i, client, cnt))
thr.append(t)
t.start()

cnt=cnt+1

for t in thr:
t.join()

threads = []
j=0
cnt=0 #variable to map the opened Vm index with the index in the ldplayer
print("in thread")
for i in range(flag,flag+no_of_vm):
threads.append(threading.Thread(target=start_end, args=(i, i+1,i, client, cnt)))
cnt=cnt+1

# Start all the threads
for t in threads:
time.sleep(random.randint(10,20))
t.start()
# Wait for all threads to finish
for t in threads:
t.join()
#################################### Main Code ##############################################

#choosing which row to access:-

if __name__ == "__main__":
##
######################### Taking all the Input values ############################
# no_of_vm=int(input("Enter the No. of Vms you want to start:---"))
no_of_vm=random.randint(7,12)
# starting_emulato=int(input("Enter from which emulator you want to start--> "))
starting_emulator=10
# ending_emulator=int(input("Enter from which emulator you want to end--> "))
ending_emulator = 199
# affilate_link=input("Enter the affilate link of the app --->")
affilate_link="https://addensuremedia.o18.click/c?o=20367141&m=2075&a=44559&aff_click_id={replace_it}&sub_aff_id={replace_it}"
# min_install_time=int(input("Enter the minimun time it will wait to tap on install button (Must be in seconds)-->"))
min_install_time=60 #60
# max_install_time=int(input("Enter the maximum time it will wait to tap on install button (Must be in seconds)-->"))
max_install_time=80 #80
# skip_opening_app=int(input("Enter out of these how many apps you want to start--> "))
skip_opening_app=ending_emulator-starting_emulator
# min_app_open_time=int(input("Enter the minimun time it will wait to open the installed app (Must be in seconds)-->"))
min_app_open_time=42
# max_app_open_time=int(input("Enter the maximun time it will wait to open the installed app (Must be in seconds)-->"))
max_app_open_time=46
# x_coordinate=int(input("Enter the X_coordinate you want to tap --> "))
x_coordinate=600
min_vid=9
max_vid=12
# y_coordinate=int(input("Enter the Y_coordinate you want to tap --> "))
y_coordinate=600
# no_of_video_watch=int(input("Enter the number of vedio you want to watch in app --> "))
#no_of_video_watch=8
flag = starting_emulator

random_number_open_app = random.sample(range(starting_emulator, ending_emulator), skip_opening_app) ## Choosing random number of opening of installed app
packagename = "com.jio.media.ondemand"
client = AdbClient(host="127.0.0.1", port=5037)

############### Runing the emulators while
while flag < ending_emulator:

thread(flag, no_of_vm, client)
print(f"Increasing flag value- by {no_of_vm}")
time.sleep(10)
os.system('ldconsole.exe quitall')
#print("closed running instances")
#time.sleep(5)
flag = flag + no_of_vm

MCQs

    1. What is the purpose of the thr list?
    [radio your-option1 use_label_element default:0 "To keep track of the thread objects" "To keep track of the emulators " "To keep track of the ADB connections " "To keep track of the installed apps]

    2. What is the role of the run_ldplayer function?

    3. What will be the output of the following code snippet?

    ```python
    logger = logging.getLogger()
    logger.setLevel(logging.DEBUG)
    app_handler = logging.FileHandler('erro.log')
    app_handler.setLevel(logging.INFO)
    formatter = logging.Formatter('%(message)s')
    app_handler.setFormatter(formatter)
    logger.addHandler(app_handler)
    logger.info('This message will be logged to erro.log')
    ```

    4. What will be the output of the following code snippet?

    ```python
    import random
    import time
    import threading
    def start_end(starting_emulator,ending_emulator,device, client, cnt):
    print(f"Started emulator -->{starting_emulator}")
    time.sleep(random.randint(1, 5))
    print(f"Ended emulator -->{ending_emulator}")

    start_end(1, 2, "device1", "client1", 1)
    start_end(3, 4, "device2", "client2", 2)
    ```

    5. What is the name of the method that creates and starts a new thread in Python?

    6. In the above code, the {run_ldplayer} function is starting the set of threads and in the {main} code at line 46 {os.system('ldconsole.exe quitall')} closes all VMs when the operation on all are completed irrespective of non synchronized completion time of VMs.

    You are required to write a demo code which can start the other VM as soon as operation on some VM is completed instead of waiting to finish all VMs.